|
| 1 | +const path = require('path') |
| 2 | +const fs = require('fs') |
| 3 | +const spawn = require('child_process').spawn |
| 4 | + |
| 5 | +/** |
| 6 | + * Spawns a child process and runs the specified command |
| 7 | + * By default, runs in the CWD and inherits stdio |
| 8 | + * Options are the same as node's child_process.spawn |
| 9 | + * @param {string} cmd |
| 10 | + * @param {array<string>} args |
| 11 | + * @param {object} options |
| 12 | + */ |
| 13 | +function runCommand(cmd, args, options) { |
| 14 | + const command = `${cmd} ${args.join(' ')}`; |
| 15 | + console.log('\nExecuting: ${command}\n') |
| 16 | + return new Promise((resolve, reject) => { |
| 17 | + const spwan = spawn( |
| 18 | + cmd, |
| 19 | + args, |
| 20 | + Object.assign( |
| 21 | + { |
| 22 | + cwd: process.cwd(), |
| 23 | + stdio: 'inherit', |
| 24 | + shell: true, |
| 25 | + }, |
| 26 | + options |
| 27 | + ) |
| 28 | + ) |
| 29 | + |
| 30 | + spwan.on('exit', () => { |
| 31 | + resolve() |
| 32 | + }) |
| 33 | + }) |
| 34 | +} |
| 35 | + |
1 | 36 | module.exports = {
|
2 | 37 | prompts: {
|
3 | 38 | name: {
|
4 |
| - type: "string", |
| 39 | + type: 'string', |
5 | 40 | required: true,
|
6 |
| - message: "Project name (kebab-case only)" |
| 41 | + message: 'Project name (kebab-case only)' |
7 | 42 | },
|
8 | 43 | description: {
|
9 |
| - type: "string", |
| 44 | + type: 'string', |
10 | 45 | required: true,
|
11 |
| - message: "Project description", |
12 |
| - default: "A Nativescript + Vue.js 2.0 project" |
| 46 | + message: 'Project description', |
| 47 | + default: 'A Nativescript + Vue.js 2.0 project' |
13 | 48 | },
|
14 | 49 | author: {
|
15 |
| - type: "string", |
16 |
| - message: "Author" |
| 50 | + type: 'string', |
| 51 | + message: 'Author' |
17 | 52 | },
|
18 | 53 | repos: {
|
19 |
| - type: "string", |
| 54 | + type: 'string', |
20 | 55 | required: true,
|
21 |
| - message: "Github repository URL", |
22 |
| - default: "user/repository" |
| 56 | + message: 'Github repository URL', |
| 57 | + default: 'user/repository' |
23 | 58 | },
|
24 | 59 | semanticRelease: {
|
25 |
| - type: "confirm", |
26 |
| - message: "Do you want to use semantic release?", |
| 60 | + type: 'confirm', |
| 61 | + message: 'Do you want to use semantic release?', |
| 62 | + default: true |
| 63 | + }, |
| 64 | + autoInstall: { |
| 65 | + type: 'confirm', |
| 66 | + message: 'Do you want to auto-install everything with npm?', |
27 | 67 | default: true
|
28 | 68 | }
|
29 | 69 | },
|
30 | 70 | filters: {
|
31 |
| - ".commitlintrc.json": "semanticRelease" |
| 71 | + '.commitlintrc.json': 'semanticRelease' |
32 | 72 | },
|
33 |
| - skipInterpolation: "src/**/*.{html,vue}", |
| 73 | + skipInterpolation: 'src/**/*.{html,vue}', |
34 | 74 | complete (data) {
|
35 |
| - console.log(data) |
36 |
| - // cd dir |
37 |
| - // npm install -g nativescript semantic-release-cli && npm install && tns init && semantic-release-cli setup |
| 75 | + const cwd = path.join(process.cwd(), data.inPlace ? '' : data.destDirName) |
| 76 | + const instructions = `please execute:\n\n$ cd ${data.destDirName} && npm run dev\n\nAnd in another shell:\n\n$ tns run [platform]`; |
| 77 | + |
| 78 | + // init global install params |
| 79 | + const params = ['install', '-g', 'nativescript'] |
| 80 | + if (data.semanticRelease) { |
| 81 | + // add semantic release |
| 82 | + params.push('semantic-release-cli') |
| 83 | + } |
| 84 | + |
| 85 | + if (data.autoInstall) { |
| 86 | + // install global packages |
| 87 | + runCommand('npm', params, {cwd}) |
| 88 | + // install project local packages |
| 89 | + .then(() => runCommand('npm', ['install'], {cwd})) |
| 90 | + // init project folder as nativescript project |
| 91 | + .then(() => runCommand('tns', ['init'], {cwd})) |
| 92 | + // install nativescript dependencies and init template |
| 93 | + .then(() => runCommand('tns', ['install'], {cwd})) |
| 94 | + // run semantic release setup if needed |
| 95 | + .then(() => (data.semanticRelease ? runCommand('semantic-release-cli', ['setup'], {cwd}) : Promise.resolve())) |
| 96 | + .then(() => console.log(`Install completed. To get started, ${instructions}`)) |
| 97 | + .catch(() => console.error('Something went wrong. Please see the log above.')) |
| 98 | + } else { |
| 99 | + // print instructions |
| 100 | + const npmParams = params.join(' ') |
| 101 | + const semanticReleaseCommand = data.semanticRelease ? '&& semantic-release-cli setup' : ''; |
| 102 | + console.log(`\nPlease execute the following script to finish the setup process :\n$ cd ${data.destDirName}\n$ npm ${npmParams} && npm install && tns init ${semanticReleaseCommand}\n\n`); |
| 103 | + console.log(`After installation completed, ${instructions}`) |
| 104 | + } |
38 | 105 | }
|
39 | 106 | }
|
0 commit comments