UNPKG

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