This repository was archived by the owner on May 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
143 lines (125 loc) · 4.19 KB
/
index.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/** Copyright (c) 2018 Uber Technologies, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
const fs = require('fs');
const mkdirpCb = require('mkdirp');
const {render} = require('nunjucks');
const {join, relative, dirname, extname} = require('path');
const {promisify} = require('util');
const readdir = require('recursive-readdir');
const {spawn} = require('child_process');
const copyFile = promisify(fs.copyFile);
const lstat = promisify(fs.lstat);
const mkdirp = promisify(mkdirpCb);
const writeFile = promisify(fs.writeFile);
/*::
type ScaffoldContext = {
cwd?: string,
path?: string,
project?: string,
projectPath?: string,
packageJsonFields?: {
[string]: any
},
};
*/
module.exports = async function scaffold(ctx /*: ScaffoldContext */ = {}) {
if (!ctx.cwd) {
ctx.cwd = process.cwd();
}
const {cwd, path, project, projectPath, packageJsonFields} = ctx;
if (!path) {
throw new Error(
"No template path was passed. There's nothing to scaffold. Please pass a `path` option specifying the directory of the scaffold template."
);
}
if (!project) {
throw new Error(
'No project name was passed. Please pass a `project` option to specify the name of your newly scaffolded project.'
);
}
const computedProjectPath = projectPath || join(cwd, project);
const templatePath = join(cwd, path);
const contentPath = join(templatePath, 'content');
await compile(computedProjectPath, templatePath, contentPath, ctx);
await updatePackageJsonFields(computedProjectPath, packageJsonFields);
await install(computedProjectPath);
};
async function updatePackageJsonFields(projectPath, packageJsonFields = null) {
if (!packageJsonFields) {
return;
}
// Setup package.json files, as we can't have these in the source code because publishing will fail.
const packageJsonPath = `${projectPath}/package.json`;
// $FlowFixMe
let packageContent = require(packageJsonPath); // eslint-disable-line import/no-dynamic-require
packageContent = {...packageContent, ...packageJsonFields};
packageContent.files = ['dist', 'src'];
fs.writeFileSync(packageJsonPath, JSON.stringify(packageContent, null, ' '));
}
async function compile(projectPath, templatePath, contentPath, ctx) {
// Get context from index.js
/* eslint-disable import/no-dynamic-require */
// $FlowFixMe
const getContext = require(join(templatePath, 'index.js'));
/* eslint-enable import/no-dynamic-require */
const newCtx = await getContext(ctx);
// Get all template files
const files = await readdir(contentPath);
return await Promise.all(
files.map(async filePath => {
const relativeFilePath = relative(contentPath, filePath);
const newFilePath = join(projectPath, relativeFilePath);
const newFileDir = dirname(newFilePath);
// Create directories to new file
await mkdirp(newFileDir);
// If is nunjucks template, compile and remove .njk extension
const fileExt = extname(newFilePath);
if (fileExt === '.njk') {
// Need to stat file for copy file mode
const fileStat = await lstat(filePath);
const compiledContents = render(filePath, newCtx);
const withoutNjkFilePath = newFilePath.replace(/\.njk$/, '');
return await writeFile(withoutNjkFilePath, compiledContents, {
mode: fileStat.mode,
});
}
// Otherwise, copy file unchanged
return await copyFile(filePath, newFilePath);
})
);
}
async function install(projectPath) {
return new Promise((resolve, reject) => {
let child;
if (!/^win/.test(process.platform)) {
// linux
child = spawn('yarn', ['install', '--cwd', projectPath], {
stdio: 'inherit',
});
} else {
// windows
child = spawn(
'cmd',
['/s', '/c', 'yarn', 'install', '--cwd', projectPath],
{
stdio: 'inherit',
}
);
}
child.on('close', code => {
if (code !== 0) {
reject(
new Error(
'Error running `yarn install`; however, the template has been successfully scaffolded.'
)
);
}
resolve();
});
});
}