UNPKG

5.4 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5var __importStar = (this && this.__importStar) || function (mod) {
6 if (mod && mod.__esModule) return mod;
7 var result = {};
8 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
9 result["default"] = mod;
10 return result;
11};
12Object.defineProperty(exports, "__esModule", { value: true });
13process.on('unhandledRejection', err => {
14 throw err;
15});
16const chalk_1 = __importDefault(require("chalk"));
17const child_process_1 = require("child_process");
18const fs = __importStar(require("fs-extra"));
19const os = __importStar(require("os"));
20const path = __importStar(require("path"));
21const argv = process.argv.slice(2);
22const createApp = (appName, useYarn) => {
23 const appPath = path.join(process.cwd(), appName);
24 console.log('Creating application ' + chalk_1.default.cyan(appName) + '\n');
25 const appPackageJson = {
26 name: appName,
27 version: '0.1.0',
28 main: 'src/index.ts',
29 private: true,
30 scripts: {
31 test: 'typescript-node-scripts test',
32 start: 'typescript-node-scripts start',
33 build: 'typescript-node-scripts build',
34 lint: 'tslint --project .',
35 },
36 };
37 const packageDevDependencies = [
38 '@types/node',
39 '@types/jest',
40 'tslint',
41 'typescript',
42 'typescript-node-scripts',
43 ];
44 // check if the path exists
45 if (fs.existsSync(appPath)) {
46 console.error();
47 console.error(chalk_1.default.red('Failed to create app.'));
48 console.error(chalk_1.default.red('Directory "' + appName + '" exists.'));
49 console.error();
50 process.exit(1);
51 }
52 // set up the template
53 fs.copySync(path.join(__dirname, '../../template'), appPath);
54 // create the package json
55 fs.writeFileSync(path.join(appPath, 'package.json'), JSON.stringify(appPackageJson, null, 2) + os.EOL);
56 // copy gitignore
57 fs.copySync(path.join(__dirname, '../../template/gitignore'), path.join(appPath, '.gitignore'));
58 fs.unlinkSync(path.join(appPath, 'gitignore'));
59 // install package dependencies
60 console.log('Installing ' +
61 [...packageDevDependencies].map(i => chalk_1.default.cyan(i)).join(', '));
62 let cmd;
63 let devDependencyArgs;
64 if (useYarn) {
65 cmd = 'yarnpkg';
66 devDependencyArgs = ['add', ...packageDevDependencies, '--dev'];
67 }
68 else {
69 cmd = 'npm';
70 devDependencyArgs = ['install', '--save-dev', ...packageDevDependencies];
71 }
72 // install dev dependencies
73 const devDependencyProc = child_process_1.spawnSync(cmd, devDependencyArgs, {
74 stdio: 'inherit',
75 cwd: appPath,
76 });
77 if (devDependencyProc.status !== 0) {
78 console.error();
79 console.error(chalk_1.default.red('Command'), chalk_1.default.redBright(`'${cmd} ${devDependencyArgs.join(' ')}'`), chalk_1.default.red('failed.'));
80 console.error();
81 process.exit(1);
82 }
83 // install dependencies
84 // const dependencyProc = spawn(cmd, dependencyArgs, {
85 // stdio: 'inherit',
86 // cwd: appPath,
87 // })
88 // if (dependencyProc.status !== 0) {
89 // console.error()
90 // console.error(
91 // chalk.red('Command'),
92 // chalk.redBright(`'${cmd} ${dependencyArgs.join(' ')}'`),
93 // chalk.red('failed.')
94 // )
95 // console.error()
96 // process.exit(1)
97 // }
98 // initialize git repo
99 try {
100 child_process_1.execSync('git init', {
101 cwd: appPath,
102 stdio: 'ignore',
103 });
104 child_process_1.execSync('git add -A', {
105 cwd: appPath,
106 stdio: 'ignore',
107 });
108 child_process_1.execSync('git commit -m "first commit by typescript-node-scripts"', {
109 cwd: appPath,
110 stdio: 'ignore',
111 });
112 }
113 catch (e) {
114 console.warn(chalk_1.default.yellow('Failed to initialize git repository. You might want to initialize it yourself.'));
115 }
116 const displayedCommand = useYarn ? 'yarn' : 'npm';
117 console.log();
118 console.log(chalk_1.default.green('Successfully created ' + appName + ' at ' + appPath));
119 console.log('Inside your project directory, you can run these commands:');
120 console.log();
121 console.log(chalk_1.default.cyan(` ${displayedCommand} start`));
122 console.log(' Starts the development server.');
123 console.log();
124 console.log(chalk_1.default.cyan(` ${displayedCommand} test`));
125 console.log(' Runs the Jest test runner.');
126 console.log();
127 console.log(chalk_1.default.cyan(` ${displayedCommand} build`));
128 console.log(' Bundles your application into a production-ready bundle.');
129 console.log();
130 console.log(chalk_1.default.green('To get started right away, we recommend running: \n'));
131 console.log(chalk_1.default.cyan(' cd'), appName);
132 console.log(chalk_1.default.cyan(` ${displayedCommand}`), 'start');
133 console.log();
134};
135const yarnAvailable = () => {
136 try {
137 child_process_1.execSync('yarnpkg --version', { stdio: 'ignore' });
138 return true;
139 }
140 catch (e) {
141 return false;
142 }
143};
144createApp(argv[0], yarnAvailable());
145//# sourceMappingURL=create.js.map
\No newline at end of file