Skip to content

Commit

Permalink
[FEATURE] add a possibility to generate subtheme for t3kit project
Browse files Browse the repository at this point in the history
  • Loading branch information
dmh committed May 29, 2017
1 parent 4fc9be1 commit f9d8f4e
Show file tree
Hide file tree
Showing 10 changed files with 379 additions and 66 deletions.
50 changes: 39 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,64 @@ const prompt = require('./lib/prompt')
const chalk = require('chalk')
const helpers = require('./lib/helpers')
// const variables = require('./lib/variables')
// const git = require('./lib/git')
const git = require('./lib/git')
const help = require('./lib/help')
const check = require('./lib/check')
// const parse = require('./lib/parse')
const parse = require('./lib/parse')
// const dep = require('./lib/dep')
const argv = require('minimist')(process.argv.slice(2))
// const cmd = require('./lib/cmd')
const cmd = require('./lib/cmd')
// const subtheme = require('./lib/subtheme')
const text = require('./lib/text')

var cache = {}

// Check is installed main dependencies
// ====================================================
check.isInstalled()

// Clone MobileApp Template
// Generate subtheme t3kit template
// ====================================================
// function subtheme () {
// return helpers.promiseChainStarter(cache)
// .then((val) => { console.log(val) })
// .catch(helpers.error)
// }
function generateSubtheme () {
return helpers.promiseChainStarter(cache)
.then(prompt.subthemeQuestions)
.then(prompt.siteName)
.then(prompt.dirName)
.then(cmd.mkdir)
.then(() => git.clone(cache, `https://github.com/t3kit/subtheme_t3kit_template.git`))
.then(() => cmd.setWorkDir(cache, cache.dirName))
.then(git.getLastTag)
.then(prompt.templateVersion)
.then(git.checkout)
.then(git.removeRepo)
.then(git.init)
.then(git.add)
.then(() => git.commit(cache, `initial commit, based on subtheme_t3kit_template v${cache.lastTag}`))
.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(() => cmd.rmFile(cache, `LICENSE.txt`))
.then(() => cmd.rmFile(cache, `CHANGELOG.md`))
.then(() => cmd.rmFile(cache, `README.md`))
.then(() => cmd.renameFile(cache, `readmeTemplate.md`, `README.md`))
.then(() => cmd.appendFile(cache, `README.md`, `**Built on [subtheme_t3kit_template](https://github.com/t3kit/subtheme_t3kit_template) v${cache.lastTag}**`))

.then(git.add)
.then(() => git.commit(cache, `initialize new subtheme_t3kit_${cache.siteName}`))

// .then((val) => { console.log(val) })
.catch(helpers.error)
}
// main start point
// ====================================================
function run () {
prompt.whatToDo(cache)
.then(() => {
if (cache.whatToDo === 'subtheme') {
console.log(chalk.red('Under construction.'))
generateSubtheme()
} else if (cache.whatToDo === 'release') {
console.log(chalk.red(text.notReady))
process.exit(1)
} else if (cache.whatToDo === 'help') {
help.allHelp()
Expand All @@ -59,7 +87,7 @@ if (_.size(argv) !== 1 || argv._.length) {
// t3kit -s, --subtheme
} else if (argv.s || argv.subtheme) {
cache.whatToDo = 'subtheme'
console.log(chalk.red('Under construction.'))
console.log(chalk.red(text.notReady))
process.exit(1)
} else {
help.allHelp()
Expand Down
12 changes: 0 additions & 12 deletions lib/check/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@ check.isInstalled = function isInstalled () {
shell.exit(1)
}

// Check is cordova installed
if (!shell.which('cordova')) {
console.log(chalk.red('Sorry, this script requires cordova'))
shell.exit(1)
}

// Check is ionic installed
if (!shell.which('ionic')) {
console.log(chalk.red('Sorry, this script requires ionic'))
shell.exit(1)
}

// Check node version
const nodeVer = variables.env.node
if (Number(process.version.match(/^v(\d+\.\d+)/)[1]) < nodeVer) {
Expand Down
62 changes: 61 additions & 1 deletion lib/cmd/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict'

var fs = require('fs')
const fs = require('fs')
const boxen = require('boxen')
const helpers = require('../helpers')

const cmd = module.exports = {}

Expand All @@ -28,3 +29,62 @@ cmd.showMessage = function showMessage (cache, message) {
resolve(cache)
})
}


// remove file
cmd.rmFile = function rmFile (cache, file) {
return new Promise(function (resolve) {
fs.unlinkSync(file)
resolve(cache)
})
}

// rename file
cmd.renameFile = function renameFile (cache, oldPath, newPath) {
return new Promise(function (resolve) {
fs.renameSync(oldPath, newPath)
resolve(cache)
})
}


// rename file
cmd.appendFile = function appendFile (cache, file, message) {
return new Promise(function (resolve, reject) {
fs.appendFile(file, message, function (err) {
if (err) {
reject(new Error(err))
}
resolve(cache)
})
})
}

// read dir recursively
cmd.readFilesRecursively = function readFilesRecursively (cache, ...dir) {
return new Promise(function (resolve) {
let files = {}
files.files = []
let getFilesRecursively = function (path, files) {
fs.readdirSync(path).forEach(function (file) {
let subpath = path + '/' + file
if (fs.lstatSync(subpath).isDirectory()) {
getFilesRecursively(subpath, files)
} else {
files.push(path + '/' + file)
}
})
}
dir.forEach(function (val) {
getFilesRecursively(val, files.files)
})
// + 1-level files
fs.readdirSync(helpers.pwd()).forEach(function (file) {
if (!fs.lstatSync(file).isDirectory()) {
files.files.push(file)
}
})
helpers.addTo(cache, files)
resolve(cache)
})
}
89 changes: 86 additions & 3 deletions lib/git/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,89 @@
// 'use strict'
'use strict'

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

// const git = module.exports = {}
const git = module.exports = {}

// clone mobileApp_template
git.clone = function clone (cache, link) {
let cmd = {
spinner: `Cloning subtheme ${cache.whatToDo}`,
str: `git clone --single-branch -b master ${link} ${cache.dirName}`,
spinerSucceed: `${cache.whatToDo} template successfully cloned`
}
return helpers.execCMD(cmd, cache)
}

// remove previous git repo
git.removeRepo = function removeRepo (cache) {
let cmd = {
// spinner: '',
str: `rm -rf .git`
// spinerSucceed: ''
}
return helpers.execCMD(cmd, cache)
}

// git add
git.init = function init (cache) {
let cmd = {
// spinner: '',
str: `git init`
// spinerSucceed: ''
}
return helpers.execCMD(cmd, cache)
}

// git add
git.add = function add (cache) {
let cmd = {
// spinner: '',
str: `git add .`
// spinerSucceed: ''
}
return helpers.execCMD(cmd, cache)
}

// git commit
git.commit = function commit (cache, commitMessage) {
let cmd = {
// spinner: '',
str: `git commit -m "${commitMessage}"`
// spinerSucceed: ''
}
return helpers.execCMD(cmd, cache)
}

// git checkout
git.checkout = function checkout (cache) {
let cmd = {
str: `git checkout ${cache.templateVersion}`
}
return helpers.execCMD(cmd, cache)
}


git.getLastTag = function getLastTag (cache) {
return new Promise(function (resolve, reject) {
let str = 'git describe --abbrev=0 --tags'
let lastTag = {}
exec(str, { maxBuffer: 2000 * 1024 }, (error, stdout) => {
if (error) {
reject(new Error(error))
} else {
if (_.isEmpty(stdout)) {
lastTag = { lastTag: false }
} else {
var match = stdout.match(/\n/i)
lastTag = stdout.slice(0, match.index)
lastTag = { lastTag: lastTag }
}
helpers.addTo(cache, lastTag)
resolve(cache)
}
})
})
}
20 changes: 3 additions & 17 deletions lib/help/index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
'use strict'

const chalk = require('chalk')
const labels = require('../labels')
// const chalk = require('chalk')
const text = require('../text')

const help = module.exports = {}

// generator help message
help.allHelp = function allHelp (cache) {
var pkgV = require('../../package.json')
let showHelp = `
${chalk.bold('t3kit-cli')} v${pkgV.version}
Command line interface
${chalk.bold.underline('Usage:')}
t3kit
t3kit [options]
${chalk.bold.underline('Options:')}
-h, --help Quick help.
-v, --version Print the t3kit-cli version.
-s, --subtheme ${labels.subtheme.description}
`
console.log(showHelp)
console.log(text.showHelp)
return cache
}

Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ helpers.promiseChainStarter = function promiseChainStarter (val) {
}

helpers.error = function stats (err) {
let divider = '==========================================\n'
let divider = `==========================================\n`
return console.log(divider + chalk.red(err.stack))
}

Expand Down
7 changes: 0 additions & 7 deletions lib/labels/index.js

This file was deleted.

25 changes: 21 additions & 4 deletions lib/parse/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
// 'use strict'
'use strict'

// const shell = require('shelljs')
// const helpers = require('../helpers')
const shell = require('shelljs')
const helpers = require('../helpers')
// const variables = require('../variables')
// var fs = require('fs')

// const parse = module.exports = {}
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', 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)
resolve(cache)
})
}
Loading

0 comments on commit f9d8f4e

Please sign in to comment.