-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgenerator.js
executable file
·109 lines (94 loc) · 3.47 KB
/
generator.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
#! /usr/bin/env node
const path = require('path');
const { execSync } = require('child_process');
const fs = require('fs-extra');
const git = require('simple-git');
const { program } = require('commander');
const appTitle = process.argv[2] || 'tram-one-app';
program
.option('--no-commit', 'skips making an initial git commit')
.option('--no-install', 'skips installing node dependencies')
.option('--url [url]', 'public url used for deployments (defaults to project name)', appTitle)
.parse();
const options = program.opts();
const processFile = (file, currentPath) => {
const filePath = path.join(currentPath, file);
const newFilePath = filePath.replace(path.join(__dirname, 'template'), path.join(process.cwd(), appTitle));
// copy a directory
if (fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()) {
// make the directory
if (fs.existsSync(newFilePath)) {
console.warn(`Folder ${newFilePath} already exists`);
} else {
fs.mkdirSync(newFilePath);
}
// process all the files in the directory
const files = fs.readdirSync(filePath);
files.forEach((file) => processFile(file, filePath));
return;
}
// copy a file
if (fs.existsSync(newFilePath)) {
console.warn(`File ${newFilePath} already exists`);
} else {
const newFile = fs.readFileSync(filePath);
if (filePath.match(/.*\.(png|ttf)/)) {
fs.appendFileSync(newFilePath, newFile);
} else if (filePath.match(/gitignore/)) {
// the gitignore file becomes an npmignore and we need to
// change it back when laying down the project
const gitignorePath = newFilePath.toString().replace('gitignore', '.gitignore');
fs.appendFileSync(gitignorePath, newFile);
} else if (filePath.match(/template-lock.json/)) {
// the package-lock.json file is ignored and we need to
// change it back when laying down the project
const packageLockPath = newFilePath.toString().replace('template-lock.json', 'package-lock.json');
fs.appendFileSync(packageLockPath, newFile);
} else {
// if it's not a binary file, treat it as a template
const templateFile = newFile
.toString()
.replace(/%TITLE%/g, appTitle)
.replace(/%URL%/g, options.url);
fs.appendFileSync(newFilePath, templateFile);
}
}
};
const init = async () => {
const filePath = path.join(__dirname, 'template');
const projectPath = path.join(process.cwd(), appTitle);
// laying down the project files
console.log(`Creating ${projectPath} `);
console.log('Copying over project files');
if (options.url !== appTitle) {
console.log(`Found unique public url, using ${options.url}`);
}
processFile('', filePath);
// making an initial commit for git
if (options.commit) {
console.log('Initializing a git repository');
try {
const simplegit = git(projectPath);
console.log('Making the initial commit');
await simplegit.init();
await simplegit.add('.');
await simplegit.commit('Initial commit from Tram-One Express');
console.log('Successfully created commit');
} catch (error) {
console.log('Failed to create commit');
}
} else {
console.log('Skipping making an initial commit');
}
// installing node dependencies
if (options.install) {
console.log('Installing NPM Depenedencies');
execSync('npm ci', { cwd: projectPath, stdio: 'inherit' });
} else {
console.log('Skipping installing NPM depenedencies, run `npm ci` to get all dependencies required');
}
console.log('');
console.log('Finished!');
console.log(`Navigate to '${appTitle}', and run 'npm start' to get started!`);
};
init();