Skip to content

Commit

Permalink
[TASK] add default dirName var, fix regex in replaceString fn
Browse files Browse the repository at this point in the history
  • Loading branch information
dmh committed May 31, 2017
1 parent 859d4f3 commit 060bb42
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 17 deletions.
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ function generateSubtheme () {

.then(prompt.subthemeQuestions)
.then(prompt.siteName)
.then(prompt.dirName)
.then(() => prompt.isOk(cache, text.subtheme.isAutoDirName(cache), prompt.dirName))
.then(cmd.mkdir)
.then(() => git.clone(cache, variables.subtheme.repo))

.then(() => cmd.setWorkDir(cache, cache.dirName))
.then(() => cmd.setWorkDir(cache, cache.dirName || variables.subtheme.dirName(cache)))
.then(git.getLastTag)
.then(prompt.templateVersion)

Expand All @@ -49,12 +49,12 @@ function generateSubtheme () {

.then(() => cmd.readFilesRecursively(cache, `Configuration`, `Resources/Private`, `Meta`))

.then(() => parse.replaceString(cache, `subtheme_t3kit_template`, `subtheme_t3kit_${cache.siteName}`))
.then(() => parse.replaceString(cache, `Subtheme t3kit template`, `Subtheme ${cache.siteName}`))
.then(() => parse.replaceString(cache, /subtheme_t3kit_template/g, `subtheme_t3kit_${cache.siteName}`))
.then(() => parse.replaceString(cache, /Subtheme t3kit template/g, `Subtheme ${cache.siteName}`))

.then(() => parse.replaceStringInFile(cache, `subtheme_t3kit_template`, `subtheme_t3kit_${cache.siteName}`, `Resources/Public/README.md`))
.then(() => parse.replaceStringInFile(cache, `subtheme_t3kit_template`, `subtheme_t3kit_${cache.siteName}`, `felayout/README.md`))
.then(() => parse.replaceStringInFile(cache, `Subtheme t3kit template`, `Subtheme ${cache.siteName}`, `felayout/README.md`))
.then(() => parse.replaceStringInFile(cache, /subtheme_t3kit_template/g, `subtheme_t3kit_${cache.siteName}`, `Resources/Public/README.md`))
.then(() => parse.replaceStringInFile(cache, /subtheme_t3kit_template/g, `subtheme_t3kit_${cache.siteName}`, `felayout/README.md`))
.then(() => parse.replaceStringInFile(cache, /Subtheme t3kit template/g, `Subtheme ${cache.siteName}`, `felayout/README.md`))

.then(() => cmd.rmFile(cache, `LICENSE.txt`))
.then(() => cmd.rmFile(cache, `CHANGELOG.md`))
Expand Down
7 changes: 6 additions & 1 deletion lib/cmd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
const fs = require('fs')
const boxen = require('boxen')
const helpers = require('../helpers')
const variables = require('../variables')

const cmd = module.exports = {}

// mkdir
cmd.mkdir = function mkdir (cache) {
return new Promise(function (resolve) {
fs.mkdirSync(cache.dirName)
if (cache.dirName) {
fs.mkdirSync(cache.dirName)
} else {
fs.mkdirSync(variables.subtheme.dirName(cache))
}
resolve(cache)
})
}
Expand Down
4 changes: 2 additions & 2 deletions lib/git/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const _ = require('lodash')
// const variables = require('../variables')
const variables = require('../variables')
const helpers = require('../helpers')
const exec = require('child_process').exec

Expand All @@ -11,7 +11,7 @@ const git = module.exports = {}
git.clone = function clone (cache, link) {
let cmd = {
spinner: `Cloning subtheme ${cache.whatToDo}`,
str: `git clone --single-branch -b master ${link} ${cache.dirName}`,
str: `git clone --single-branch -b master ${link} ${cache.dirName || variables.subtheme.dirName(cache)}`,
spinerSucceed: `${cache.whatToDo} template successfully cloned`
}
return helpers.execCMD(cmd, cache)
Expand Down
6 changes: 4 additions & 2 deletions lib/parse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ const parse = module.exports = {}
// replace string in files
parse.replaceString = function replaceString (cache, oldString, newString) {
return new Promise(function (resolve) {
shell.sed('-i', helpers.escapeRegExp(oldString), newString, cache.files)
// shell.sed('-i', helpers.escapeRegExp(oldString), newString, cache.files)
shell.sed('-i', oldString, newString, cache.files)
resolve(cache)
})
}

parse.replaceStringInFile = function replaceStringInFile (cache, oldString, newString, file) {
return new Promise(function (resolve) {
shell.sed('-i', helpers.escapeRegExp(oldString), newString, file)
// shell.sed('-i', helpers.escapeRegExp(oldString), newString, file)
shell.sed('-i', oldString, newString, file)
resolve(cache)
})
}
31 changes: 29 additions & 2 deletions lib/prompt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const helpers = require('../helpers')
const chalk = require('chalk')
var fs = require('fs')
const text = require('../text')
const variables = require('../variables')

const prompt = module.exports = {}

Expand Down Expand Up @@ -99,7 +100,7 @@ prompt.dirName = function dirName (cache) {
return value.trim()
},
default: function () {
return `subtheme_t3kit_${cache.siteName}`
return variables.subtheme.dirName(cache)
},
validate: function (value) {
if (!fs.existsSync(value)) {
Expand Down Expand Up @@ -159,11 +160,37 @@ prompt.isOk = function isOk (cache, message) {
inquirer.prompt(isOk)
.then(function (answers) {
if (answers.isOk) {
// helpers.addTo(cache, answers)
resolve(cache)
} else {
process.exit(1)
}
})
})
}

prompt.isOk = function isOk (cache, message, fn) {
return new Promise(function (resolve, reject) {
let isOk = [
{
name: `isOk`,
type: 'confirm',
message: message
}
]
inquirer.prompt(isOk)
.then(function (answers) {
if (answers.isOk) {
resolve(cache)
} else {
if (fn) {
// console.log(fn)
// console.log(fn())
// fn(cache)
resolve(fn(cache))
} else {
process.exit(1)
}
}
})
})
}
6 changes: 4 additions & 2 deletions lib/text/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const chalk = require('chalk')
const variables = require('../variables')

const helpers = require('../helpers')
const text = module.exports = {}
Expand All @@ -10,7 +11,7 @@ var pkgV = require('../../package.json')
text.notReady = `Under construction...`

text.subtheme = {
description: `Generate new subtheme_t3kit_name`,
description: `Generate new subtheme for t3kit project`,

templateVersion: `Which subtheme_t3kit_template version you want to use?`,
templateVersionMaster: `Last commit on ${chalk.blue('master')} branch`,
Expand All @@ -19,7 +20,8 @@ text.subtheme = {
siteName: `Site name:`,
siteNameIsOk: `Check site name one more time. Is it correct?`,

dirName: `Create a new folder with name:`,
dirName: `Create custom folder with name:`,
isAutoDirName: (cache) => `Automaticaly create folder ${chalk.yellow(`${variables.subtheme.dirName(cache)}`)} ?`,
done (cache) {
return `
${chalk.bold('Done!')}
Expand Down
3 changes: 2 additions & 1 deletion lib/variables/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ variables.env = {

variables.subtheme = {
repo: `https://github.com/t3kit/subtheme_t3kit_template.git`,
readmeLabel: (cache) => `**Built on [subtheme_t3kit_template](https://github.com/t3kit/subtheme_t3kit_template) v${cache.lastTag}**`
readmeLabel: (cache) => `**Built on [subtheme_t3kit_template](https://github.com/t3kit/subtheme_t3kit_template) v${cache.lastTag}**`,
dirName: (cache) => `subtheme_t3kit_${cache.siteName}`
}

0 comments on commit 060bb42

Please sign in to comment.