Skip to content

Commit

Permalink
feat: add init command
Browse files Browse the repository at this point in the history
  • Loading branch information
Beace committed Jul 1, 2019
1 parent 2b8d021 commit 803ecdd
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 5 deletions.
4 changes: 2 additions & 2 deletions bin/trust-scripts-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ program
.option("-f, --folder <folder>", "初始化项目目录")
.parse(process.argv);

const dev = require("../lib/init");
const init = require("../lib/init");

dev({ ...program });
init({ ...program });
57 changes: 57 additions & 0 deletions lib/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const path = require("path");
const fs = require("fs");

const chalk = require("chalk");
const shelljs = require("shelljs");
const prompts = require('prompts');

const DEFAULT_FOLDER = "trust-scripts-demo";

const init = async ({ folder = "trust-scripts-demo" }) => {
console.log();
const response = await prompts({
type: 'text',
name: 'folder',
message: 'Name your project?',
initial: DEFAULT_FOLDER,
});

const projectName = response.folder || folder;

const cwd = process.cwd();
const templatePath = path.join(__dirname, '../', 'template');
const projectPath = path.join(cwd, folder);
if (!fs.existsSync(projectPath)) {
console.log(
chalk.green("INFO:"),
`PATH: ${chalk.yellow(projectPath)} not exists`
);

console.log(chalk.green("- Create folder..."));
shelljs.mkdir(projectPath);
console.log(chalk.green(`√ Create Success ${projectPath}!`));
}

console.log(chalk.green('- Copy the template...'));
shelljs.cp("-r", `${templatePath}/*`, projectPath);
console.log(chalk.green("√ Copy Success!"));

console.log(chalk.green("- Run npm install..."))
shelljs.cd(projectName);
if (shelljs.exec('npm install').code !== 0) {
shelljs.echo('Error: npm install failed');
shelljs.exit(1);
process.exit(1);
}
console.log(chalk.green("\n√ NPM install succeed!"))
console.log(chalk.blue("\n========================================================================================"))
console.log(chalk.blue('\nWelcome to use trust-scripts!!!'))
console.log(chalk.blue('Follow the next steps, and build your awesome project without caring about how to build\n'))
console.log(chalk.green(`1. cd ${folder}`));
// console.log(chalk.green(`2. npm install`));
console.log(chalk.green(`2. npm start`));
console.log(chalk.green(`3. npm run build`));
console.log(chalk.blue("\n========================================================================================"))
};

module.exports = init;
46 changes: 43 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
"mini-css-extract-plugin": "^0.7.0",
"mkcert": "^1.2.0",
"optimize-css-assets-webpack-plugin": "^5.0.1",
"prompts": "^2.1.0",
"shelljs": "^0.8.3",
"style-loader": "^0.23.1",
"webpack-simple-progress-plugin": "0.0.4"
}
Expand Down

0 comments on commit 803ecdd

Please sign in to comment.