-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscaffold-it-template
More file actions
executable file
·97 lines (77 loc) · 3.11 KB
/
scaffold-it-template
File metadata and controls
executable file
·97 lines (77 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env node
'use strict';
const chalk = require('chalk');
const execSync = require('child_process').execSync;
const fs = require('fs');
const inquirer = require('inquirer');
const os = require('os');
const path = require('path');
const program = require('commander');
const scaffold = require('scaffold-generator');
program
.option('-o, --override', 'Override any existing files')
.option('-p, --open [value]', 'Open template characters, Defaults to {{{%')
.option('-c, --close [value]', 'Close template characters. Defaults to %}}}')
.action(action);
function action(templateName, desiredDestination) {
const destination = path.resolve(desiredDestination);
const source = path.join(os.homedir(), '.scaffold-it', templateName);
const hooksDir = path.join(source, 'hooks');
const ignoreFile = path.join(source, 'globignore.js');
const ignore = fs.existsSync(ignoreFile) ? require(ignoreFile) : undefined;
const tplDir = path.join(source, 'template');
const variables = require(path.join(tplDir, 'template-variables.json'));
const hooks = {
pre: 'pre.hook',
post: 'post.hook'
};
let questions = [];
for (let key of Object.keys(variables)) {
questions.push({
type : 'input',
name : key,
message: key + (variables[key].description ? ` - ${variables[key].description}` : '')
})
}
inquirer
.prompt(questions)
.then(answers => {
console.log(chalk.yellow(`This ${program.override ? 'will' : 'will not'} override existing files`));
console.log(chalk.green('Data used for templating:'));
console.log(chalk.cyan(JSON.stringify(answers, null, 4)));
//run pre hook, if exists
let preHook = path.join(hooksDir, hooks.pre);
runHook(preHook, source, answers);
scaffold({
data: answers,
open: program.open || '{{{%',
close: program.close || '%}}}',
override: program.override,
noBackup: program.override,
ignore: ignore
})
.copy(tplDir, destination, err => {
if (err) {
console.log(chalk.red('ERROR:'));
console.log(err);
} else {
console.log(chalk.green('Scaffolding complete.'));
console.log(chalk.green(`New project is at: ${destination}`));
//run post hook, if exists
let postHook = path.join(hooksDir, hooks.post);
runHook(postHook, destination, answers);
}
});
});
}
function runHook(hook, cwd, answers) {
console.log(chalk.yellow(`Looking for hook ${hook}`));
if (!fs.existsSync(hook)) {
console.log(chalk.yellow('No such hook'));
return false;
}
console.log(chalk.yellow('Running hoook'));
let cmd = `${hook} ${JSON.stringify(answers)}`;
execSync(cmd, {cwd: cwd, stdio: 'inherit'});
}
program.parse(process.argv);