@quilted/create 0.1.11 → 0.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/build/cjs/app.cjs +303 -0
- package/build/cjs/index.cjs +5220 -5198
- package/build/cjs/index2.cjs +374 -0
- package/build/cjs/index3.cjs +50060 -0
- package/build/cjs/index4.cjs +7854 -0
- package/build/cjs/minimatch.cjs +1202 -0
- package/build/cjs/package-manager.cjs +300 -0
- package/build/cjs/package.cjs +410 -0
- package/build/esm/app.mjs +280 -0
- package/build/esm/index.mjs +5206 -5195
- package/build/esm/index2.mjs +365 -0
- package/build/esm/index3.mjs +50047 -0
- package/build/esm/index4.mjs +7852 -0
- package/build/esm/minimatch.mjs +1200 -0
- package/build/esm/package-manager.mjs +270 -0
- package/build/esm/package.mjs +387 -0
- package/build/tsconfig.tsbuildinfo +1 -1
- package/build/typescript/app.d.ts +2 -0
- package/build/typescript/app.d.ts.map +1 -0
- package/build/typescript/help.d.ts +6 -0
- package/build/typescript/help.d.ts.map +1 -0
- package/build/typescript/package.d.ts +2 -0
- package/build/typescript/package.d.ts.map +1 -0
- package/build/typescript/shared/package-manager.d.ts +3 -0
- package/build/typescript/shared/package-manager.d.ts.map +1 -0
- package/build/typescript/shared/prompts.d.ts +22 -0
- package/build/typescript/shared/prompts.d.ts.map +1 -0
- package/build/typescript/shared/tsconfig.d.ts +3 -0
- package/build/typescript/shared/tsconfig.d.ts.map +1 -0
- package/build/typescript/shared.d.ts +21 -0
- package/build/typescript/shared.d.ts.map +1 -0
- package/package.json +13 -3
- package/source/app.ts +387 -0
- package/source/create.ts +43 -383
- package/source/help.ts +132 -0
- package/source/package.ts +510 -0
- package/source/shared/package-manager.ts +74 -0
- package/source/shared/prompts.ts +133 -0
- package/source/shared/tsconfig.ts +90 -0
- package/source/shared.ts +165 -0
- package/templates/{app → app-basic}/App.tsx +4 -7
- package/templates/{app → app-basic}/features/Start/Start.module.css +0 -0
- package/templates/{app → app-basic}/features/Start/Start.tsx +1 -1
- package/templates/{app → app-basic}/features/Start/index.ts +0 -0
- package/templates/{app → app-basic}/foundation/Head.tsx +8 -8
- package/templates/{app → app-basic}/foundation/Http.tsx +10 -6
- package/templates/{app → app-basic}/package.json +10 -1
- package/templates/{app → app-basic}/quilt.project.ts +0 -0
- package/templates/{app/server.ts → app-basic/server.tsx} +1 -1
- package/templates/{app → app-basic}/shared/types.ts +0 -0
- package/templates/{app → app-basic}/tsconfig.json +0 -0
- package/templates/app-single-file/App.tsx +168 -0
- package/templates/app-single-file/package.json +30 -0
- package/templates/app-single-file/quilt.project.ts +6 -0
- package/templates/app-single-file/tsconfig.json +9 -0
- package/templates/{workspace → github}/_github/workflows/actions/prepare/action.yml +0 -0
- package/templates/{workspace → github}/_github/workflows/ci.yml +0 -0
- package/templates/package/package.json +24 -9
- package/templates/package/quilt.project.ts +1 -1
- package/templates/vscode/_vscode/extensions.json +7 -0
- package/templates/vscode/_vscode/settings.json +17 -0
- package/templates/workspace/_gitignore +1 -1
- package/templates/workspace/_prettierignore +1 -0
- package/templates/workspace/package.json +1 -6
- package/templates/workspace/tsconfig.json +6 -1
- package/templates/workspace/pnpm-workspace.yaml +0 -3
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import * as path from 'path';
|
|
2
|
+
import { relative } from 'path';
|
|
3
|
+
import * as fs from 'fs';
|
|
4
|
+
import { fileURLToPath } from 'url';
|
|
5
|
+
import './index.mjs';
|
|
6
|
+
|
|
7
|
+
function loadTemplate(name) {
|
|
8
|
+
let templateRootPromise;
|
|
9
|
+
return {
|
|
10
|
+
async copy(to, handleFile) {
|
|
11
|
+
templateRootPromise ?? (templateRootPromise = templateDirectory(name));
|
|
12
|
+
const templateRoot = await templateRootPromise;
|
|
13
|
+
const targetRoot = path.resolve(to);
|
|
14
|
+
const files = fs.readdirSync(templateRoot).filter(file => !path.basename(file).startsWith('.'));
|
|
15
|
+
|
|
16
|
+
for (const file of files) {
|
|
17
|
+
if (handleFile) {
|
|
18
|
+
if (!handleFile(file)) {
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const targetPath = path.join(targetRoot, file.startsWith('_') ? `.${file.slice(1)}` : file);
|
|
24
|
+
copy(path.join(templateRoot, file), targetPath);
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
async read(file) {
|
|
29
|
+
templateRootPromise ?? (templateRootPromise = templateDirectory(name));
|
|
30
|
+
const templateRoot = await templateRootPromise;
|
|
31
|
+
return fs.readFileSync(path.join(templateRoot, file), {
|
|
32
|
+
encoding: 'utf8'
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function createOutputTarget(target) {
|
|
39
|
+
return {
|
|
40
|
+
root: target,
|
|
41
|
+
|
|
42
|
+
read(file) {
|
|
43
|
+
return fs.promises.readFile(path.resolve(target, file), {
|
|
44
|
+
encoding: 'utf8'
|
|
45
|
+
});
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
async write(file, content) {
|
|
49
|
+
await writeFile(path.resolve(target, file), content);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
let packageRootPromise;
|
|
55
|
+
|
|
56
|
+
async function templateDirectory(name) {
|
|
57
|
+
return path.join(await getPackageRoot(), 'templates', name);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function getPackageRoot() {
|
|
61
|
+
if (!packageRootPromise) {
|
|
62
|
+
packageRootPromise = (async () => {
|
|
63
|
+
const {
|
|
64
|
+
packageDirectory
|
|
65
|
+
} = await import('./index2.mjs');
|
|
66
|
+
return packageDirectory({
|
|
67
|
+
cwd: path.dirname(fileURLToPath(import.meta.url))
|
|
68
|
+
});
|
|
69
|
+
})();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return packageRootPromise;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function toValidPackageName(projectName) {
|
|
76
|
+
return projectName.trim().toLowerCase().replace(/\s+/g, '-').replace(/^[._]/, '').replace(/[^a-z0-9-~@/]+/g, '-');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function copy(source, destination) {
|
|
80
|
+
const stat = fs.statSync(source);
|
|
81
|
+
|
|
82
|
+
if (stat.isDirectory()) {
|
|
83
|
+
copyDirectory(source, destination);
|
|
84
|
+
} else {
|
|
85
|
+
fs.copyFileSync(source, destination);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function copyDirectory(source, destination) {
|
|
90
|
+
fs.mkdirSync(destination, {
|
|
91
|
+
recursive: true
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
for (const file of fs.readdirSync(source)) {
|
|
95
|
+
const srcFile = path.resolve(source, file);
|
|
96
|
+
const destFile = path.resolve(destination, file);
|
|
97
|
+
copy(srcFile, destFile);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async function writeFile(file, content) {
|
|
102
|
+
await fs.promises.writeFile(file, content);
|
|
103
|
+
}
|
|
104
|
+
async function isEmpty(path) {
|
|
105
|
+
return fs.readdirSync(path).length === 0;
|
|
106
|
+
}
|
|
107
|
+
async function emptyDirectory(dir) {
|
|
108
|
+
if (!fs.existsSync(dir)) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
for (const file of fs.readdirSync(dir)) {
|
|
113
|
+
fs.rmSync(path.resolve(dir, file), {
|
|
114
|
+
force: true,
|
|
115
|
+
recursive: true
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function relativeDirectoryForDisplay(relativeDirectory) {
|
|
120
|
+
return relativeDirectory.startsWith('.') ? relativeDirectory : `.${path.sep}${relativeDirectory}`;
|
|
121
|
+
}
|
|
122
|
+
async function format(content, {
|
|
123
|
+
as: parser
|
|
124
|
+
}) {
|
|
125
|
+
const [{
|
|
126
|
+
format
|
|
127
|
+
}] = await Promise.all([import('./index3.mjs').then(function (n) { return n.i; })]);
|
|
128
|
+
return format(content, {
|
|
129
|
+
arrowParens: 'always',
|
|
130
|
+
bracketSpacing: false,
|
|
131
|
+
singleQuote: true,
|
|
132
|
+
trailingComma: 'all',
|
|
133
|
+
parser
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const ENDS_WITH_TSCONFIG = /[/]?tsconfig[.a-z0-9]*[.]json/i;
|
|
138
|
+
async function addToTsConfig(directory, output) {
|
|
139
|
+
const tsconfig = JSON.parse(await output.read('tsconfig.json'));
|
|
140
|
+
tsconfig.references ?? (tsconfig.references = []);
|
|
141
|
+
const relativePath = relative(output.root, directory);
|
|
142
|
+
const relativeForDisplay = relativeDirectoryForDisplay(relativePath);
|
|
143
|
+
|
|
144
|
+
if (tsconfig.references.length === 0) {
|
|
145
|
+
tsconfig.references.push({
|
|
146
|
+
path: relativeForDisplay
|
|
147
|
+
});
|
|
148
|
+
} else {
|
|
149
|
+
let hasExistingReference = false;
|
|
150
|
+
let referenceFormat = 'pretty-relative';
|
|
151
|
+
|
|
152
|
+
for (const {
|
|
153
|
+
path
|
|
154
|
+
} of tsconfig.references) {
|
|
155
|
+
if (path.startsWith('./')) {
|
|
156
|
+
if (ENDS_WITH_TSCONFIG.test(path)) {
|
|
157
|
+
referenceFormat = 'pretty-tsconfig';
|
|
158
|
+
|
|
159
|
+
if (path === `${relativeForDisplay}/tsconfig.json`) {
|
|
160
|
+
hasExistingReference = true;
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
referenceFormat = 'pretty-relative';
|
|
166
|
+
|
|
167
|
+
if (path === relativeForDisplay) {
|
|
168
|
+
hasExistingReference = true;
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
} else if (ENDS_WITH_TSCONFIG.test(path)) {
|
|
172
|
+
referenceFormat = 'tsconfig';
|
|
173
|
+
|
|
174
|
+
if (path === `${relativePath}/tsconfig.json`) {
|
|
175
|
+
hasExistingReference = true;
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
} else {
|
|
179
|
+
referenceFormat = 'relative';
|
|
180
|
+
|
|
181
|
+
if (path === relativePath) {
|
|
182
|
+
hasExistingReference = true;
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (!hasExistingReference) {
|
|
189
|
+
let path;
|
|
190
|
+
|
|
191
|
+
if (referenceFormat === 'pretty-tsconfig') {
|
|
192
|
+
path = `${relativeForDisplay}/tsconfig.json`;
|
|
193
|
+
} else if (referenceFormat === 'pretty-relative') {
|
|
194
|
+
path = relativeForDisplay;
|
|
195
|
+
} else if (referenceFormat === 'tsconfig') {
|
|
196
|
+
path = `${relativePath}/tsconfig.json`;
|
|
197
|
+
} else {
|
|
198
|
+
path = relativePath;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
tsconfig.references.push({
|
|
202
|
+
path
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
tsconfig.references = tsconfig.references.sort(({
|
|
208
|
+
path: pathOne
|
|
209
|
+
}, {
|
|
210
|
+
path: pathTwo
|
|
211
|
+
}) => pathOne.localeCompare(pathTwo));
|
|
212
|
+
await output.write('tsconfig.json', await format(JSON.stringify(tsconfig), {
|
|
213
|
+
as: 'json'
|
|
214
|
+
}));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async function addToPackageManagerWorkspaces(directory, output, packageManager) {
|
|
218
|
+
if (packageManager === 'pnpm') {
|
|
219
|
+
const {
|
|
220
|
+
parse,
|
|
221
|
+
stringify
|
|
222
|
+
} = await import('./index4.mjs').then(function (n) { return n.i; });
|
|
223
|
+
const workspaceYaml = parse(await output.read('pnpm-workspace.yaml'));
|
|
224
|
+
workspaceYaml.packages = await addToWorkspaces(relative(output.root, directory), workspaceYaml.packages ?? []);
|
|
225
|
+
await output.write('pnpm-workspace.yaml', await format(stringify(workspaceYaml), {
|
|
226
|
+
as: 'yaml'
|
|
227
|
+
}));
|
|
228
|
+
} else {
|
|
229
|
+
const packageJson = JSON.parse(await output.read('package.json'));
|
|
230
|
+
packageJson.workspaces = await addToWorkspaces(relative(output.root, directory), packageJson.workspaces ?? []);
|
|
231
|
+
await output.write('package.json', await format(JSON.stringify(packageJson), {
|
|
232
|
+
as: 'json-stringify'
|
|
233
|
+
}));
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
async function addToWorkspaces(relative, workspaces) {
|
|
238
|
+
if (workspaces.length === 0) {
|
|
239
|
+
return [relative];
|
|
240
|
+
} // Default documentation seems to generally exclude leading `./` on paths
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
let pretty = false;
|
|
244
|
+
let hasMatch = false;
|
|
245
|
+
const {
|
|
246
|
+
default: minimatch
|
|
247
|
+
} = await import('./minimatch.mjs').then(function (n) { return n.m; });
|
|
248
|
+
|
|
249
|
+
for (const pattern of workspaces) {
|
|
250
|
+
let normalizedPattern = pattern;
|
|
251
|
+
|
|
252
|
+
if (pattern.startsWith('./')) {
|
|
253
|
+
pretty = true;
|
|
254
|
+
normalizedPattern = pattern.slice(2);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (minimatch(relative, normalizedPattern)) {
|
|
258
|
+
hasMatch = true;
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (hasMatch) {
|
|
264
|
+
return workspaces;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return [...workspaces, pretty ? relativeDirectoryForDisplay(relative) : relative].sort((patternOne, patternTwo) => patternOne.localeCompare(patternTwo));
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export { addToTsConfig as a, addToPackageManagerWorkspaces as b, createOutputTarget as c, emptyDirectory as e, format as f, isEmpty as i, loadTemplate as l, relativeDirectoryForDisplay as r, toValidPackageName as t };
|
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
import { execSync } from 'child_process';
|
|
4
|
+
import { s as stripIndent, c as cyan_1, p as printHelp, g as getCreateAsMonorepo, a as getShouldInstall, d as getPackageManager, e as getExtrasToSetup, f as dim_1, u as underline_1, m as magenta_1, h as arg_1, i as prompt, b as bold_1 } from './index.mjs';
|
|
5
|
+
import { t as toValidPackageName, e as emptyDirectory, f as format, l as loadTemplate, a as addToTsConfig, b as addToPackageManagerWorkspaces, r as relativeDirectoryForDisplay, i as isEmpty, c as createOutputTarget } from './package-manager.mjs';
|
|
6
|
+
import 'tty';
|
|
7
|
+
import 'url';
|
|
8
|
+
import 'readline';
|
|
9
|
+
import 'events';
|
|
10
|
+
|
|
11
|
+
async function createPackage() {
|
|
12
|
+
const argv = getArgv();
|
|
13
|
+
|
|
14
|
+
if (argv['--help']) {
|
|
15
|
+
const additionalOptions = stripIndent`
|
|
16
|
+
${cyan_1(`--react`)}, ${cyan_1(`--no-react`)}
|
|
17
|
+
Whether this package will use React. If you don’t provide this option, the command
|
|
18
|
+
will ask you about it later.
|
|
19
|
+
|
|
20
|
+
${cyan_1(`--public`)}, ${cyan_1(`--private`)}
|
|
21
|
+
Whether this package will be published for other projects to install. If you do not
|
|
22
|
+
provide this option, the command will ask you about it later.
|
|
23
|
+
|
|
24
|
+
${cyan_1(`--registry`)}
|
|
25
|
+
The package registry to publish this package to. This option only applies if you create
|
|
26
|
+
a public package. If you do not provide this option, it will use the default NPM registry.
|
|
27
|
+
`;
|
|
28
|
+
printHelp({
|
|
29
|
+
kind: 'package',
|
|
30
|
+
options: additionalOptions,
|
|
31
|
+
packageManager: argv['--package-manager']?.toLowerCase()
|
|
32
|
+
});
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const inWorkspace = fs.existsSync('quilt.workspace.ts');
|
|
37
|
+
const name = await getName(argv);
|
|
38
|
+
const directory = await getDirectory(argv, {
|
|
39
|
+
name,
|
|
40
|
+
inWorkspace
|
|
41
|
+
});
|
|
42
|
+
const isPublic = await getPublic(argv);
|
|
43
|
+
const useReact = await getReact(argv);
|
|
44
|
+
const createAsMonorepo = !inWorkspace && (await getCreateAsMonorepo(argv));
|
|
45
|
+
const shouldInstall = await getShouldInstall(argv);
|
|
46
|
+
const packageManager = await getPackageManager(argv);
|
|
47
|
+
const setupExtras = await getExtrasToSetup(argv, {
|
|
48
|
+
inWorkspace
|
|
49
|
+
});
|
|
50
|
+
const partOfMonorepo = inWorkspace || createAsMonorepo;
|
|
51
|
+
const packageDirectory = createAsMonorepo ? path.join(directory, `packages/${toValidPackageName(name.split('/').pop())}`) : directory;
|
|
52
|
+
|
|
53
|
+
if (fs.existsSync(directory)) {
|
|
54
|
+
await emptyDirectory(directory);
|
|
55
|
+
|
|
56
|
+
if (packageDirectory !== directory) {
|
|
57
|
+
fs.mkdirSync(packageDirectory, {
|
|
58
|
+
recursive: true
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
} else {
|
|
62
|
+
fs.mkdirSync(packageDirectory, {
|
|
63
|
+
recursive: true
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const rootDirectory = inWorkspace ? process.cwd() : directory;
|
|
68
|
+
const outputRoot = createOutputTarget(rootDirectory);
|
|
69
|
+
const packageTemplate = loadTemplate('package');
|
|
70
|
+
const workspaceTemplate = loadTemplate('workspace');
|
|
71
|
+
let quiltProject = await packageTemplate.read('quilt.project.ts');
|
|
72
|
+
|
|
73
|
+
if (useReact) {
|
|
74
|
+
quiltProject = quiltProject.replace('react: false', 'react: true');
|
|
75
|
+
} // If we aren’t already in a workspace, copy the workspace files over, which
|
|
76
|
+
// are needed if we are making a monorepo or not.
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
if (!inWorkspace) {
|
|
80
|
+
await workspaceTemplate.copy(directory, file => {
|
|
81
|
+
// When this is a single project, we use the project’s Quilt configuration as the base.
|
|
82
|
+
if (file === 'quilt.workspace.ts') return createAsMonorepo; // We need to make some adjustments to the root package.json
|
|
83
|
+
|
|
84
|
+
return file !== 'package.json';
|
|
85
|
+
}); // If we are creating a monorepo, we need to add the root package.json and
|
|
86
|
+
// package manager workspace configuration.
|
|
87
|
+
|
|
88
|
+
if (createAsMonorepo) {
|
|
89
|
+
const workspacePackageJson = JSON.parse(await workspaceTemplate.read('package.json'));
|
|
90
|
+
workspacePackageJson.name = toValidPackageName(name);
|
|
91
|
+
|
|
92
|
+
if (packageManager === 'pnpm') {
|
|
93
|
+
await outputRoot.write('pnpm-workspace.yaml', await format(`
|
|
94
|
+
packages:
|
|
95
|
+
- './packages/*'
|
|
96
|
+
`, {
|
|
97
|
+
as: 'yaml'
|
|
98
|
+
}));
|
|
99
|
+
} else {
|
|
100
|
+
workspacePackageJson.workspaces = ['packages/*'];
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
await outputRoot.write('package.json', await format(JSON.stringify(workspacePackageJson), {
|
|
104
|
+
as: 'json-stringify'
|
|
105
|
+
}));
|
|
106
|
+
} else {
|
|
107
|
+
const [projectPackageJson, projectTSConfig, workspacePackageJson] = await Promise.all([packageTemplate.read('package.json').then(content => JSON.parse(content)), packageTemplate.read('tsconfig.json').then(content => JSON.parse(content)), workspaceTemplate.read('package.json').then(content => JSON.parse(content))]);
|
|
108
|
+
workspacePackageJson.eslintConfig = projectPackageJson.eslintConfig;
|
|
109
|
+
workspacePackageJson.browserslist = projectPackageJson.browserslist;
|
|
110
|
+
const newPackageJson = {}; // We want to put the project’s dependencies in the package.json, respecting
|
|
111
|
+
// the preferred ordering (dependencies, peer dependencies, dev dependencies).
|
|
112
|
+
|
|
113
|
+
for (const [key, value] of Object.entries(projectPackageJson)) {
|
|
114
|
+
if (key !== 'devDependencies') {
|
|
115
|
+
newPackageJson[key] = value;
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
newPackageJson.dependencies = projectPackageJson.dependencies;
|
|
120
|
+
newPackageJson.peerDependencies = projectPackageJson.peerDependencies;
|
|
121
|
+
newPackageJson.peerDependenciesMeta = projectPackageJson.peerDependenciesMeta;
|
|
122
|
+
newPackageJson.devDependencies = sortKeys({ ...workspacePackageJson.devDependencies,
|
|
123
|
+
...projectPackageJson.devDependencies
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
adjustPackageJson(newPackageJson, {
|
|
128
|
+
name: toValidPackageName(name),
|
|
129
|
+
react: useReact,
|
|
130
|
+
isPublic,
|
|
131
|
+
registry: argv['--registry']
|
|
132
|
+
});
|
|
133
|
+
const addBackToTSConfigInclude = new Set(['quilt.project.ts', '*.test.ts', '*.test.tsx']);
|
|
134
|
+
projectTSConfig.exclude = projectTSConfig.exclude.filter(excluded => !addBackToTSConfigInclude.has(excluded));
|
|
135
|
+
quiltProject = quiltProject.replace('quiltPackage', 'quiltWorkspace, quiltPackage').replace('quiltPackage(', 'quiltWorkspace(), quiltPackage(');
|
|
136
|
+
await outputRoot.write('quilt.project.ts', await format(quiltProject, {
|
|
137
|
+
as: 'typescript'
|
|
138
|
+
}));
|
|
139
|
+
await outputRoot.write('package.json', await format(JSON.stringify(newPackageJson), {
|
|
140
|
+
as: 'json-stringify'
|
|
141
|
+
}));
|
|
142
|
+
await outputRoot.write('tsconfig.json', await format(JSON.stringify(projectTSConfig), {
|
|
143
|
+
as: 'json'
|
|
144
|
+
}));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (setupExtras.has('github')) {
|
|
148
|
+
await loadTemplate('github').copy(directory);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (setupExtras.has('vscode')) {
|
|
152
|
+
await loadTemplate('vscode').copy(directory);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
await packageTemplate.copy(packageDirectory, file => {
|
|
157
|
+
// If we are in a monorepo, we can use all the template files as they are
|
|
158
|
+
if (file === 'tsconfig.json') {
|
|
159
|
+
return partOfMonorepo;
|
|
160
|
+
} // We need to make some adjustments the project’s package.json and Quilt config
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
return file !== 'package.json' && file !== 'quilt.project.ts';
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
if (partOfMonorepo) {
|
|
167
|
+
// Write the package’s package.json (the root one was already created)
|
|
168
|
+
const projectPackageJson = JSON.parse(await packageTemplate.read('package.json'));
|
|
169
|
+
adjustPackageJson(projectPackageJson, {
|
|
170
|
+
name: toValidPackageName(name),
|
|
171
|
+
react: useReact,
|
|
172
|
+
isPublic,
|
|
173
|
+
registry: argv['--registry']
|
|
174
|
+
});
|
|
175
|
+
await outputRoot.write(path.join(packageDirectory, 'package.json'), await format(JSON.stringify(projectPackageJson), {
|
|
176
|
+
as: 'json-stringify'
|
|
177
|
+
}));
|
|
178
|
+
await Promise.all([addToTsConfig(packageDirectory, outputRoot), addToPackageManagerWorkspaces(packageDirectory, outputRoot, packageManager)]);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (shouldInstall) {
|
|
182
|
+
process.stdout.write('\nInstalling dependencies...\n'); // TODO: better loading, handle errors
|
|
183
|
+
|
|
184
|
+
execSync(`${packageManager} install`, {
|
|
185
|
+
cwd: rootDirectory
|
|
186
|
+
});
|
|
187
|
+
process.stdout.moveCursor(0, -1);
|
|
188
|
+
process.stdout.clearLine(1);
|
|
189
|
+
console.log('Installed dependencies.');
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const commands = [];
|
|
193
|
+
|
|
194
|
+
if (!inWorkspace && directory !== process.cwd()) {
|
|
195
|
+
commands.push(`cd ${cyan_1(relativeDirectoryForDisplay(path.relative(process.cwd(), directory)))} ${dim_1('# Move into your new package’s directory')}`);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (!shouldInstall) {
|
|
199
|
+
commands.push(`pnpm install ${dim_1('# Install all your dependencies')}`);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (!inWorkspace) {
|
|
203
|
+
// TODO: change this condition to check if git was initialized already
|
|
204
|
+
commands.push(`git init && git add -A && git commit -m "Initial commit" ${dim_1('# Start your git history (optional)')}`);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const whatsNext = stripIndent`
|
|
208
|
+
Your new package is ready to go! There’s just ${commands.length > 1 ? 'a few more steps' : 'one more step'} you’ll need to take
|
|
209
|
+
in order to start building:
|
|
210
|
+
`;
|
|
211
|
+
console.log();
|
|
212
|
+
console.log(whatsNext);
|
|
213
|
+
console.log();
|
|
214
|
+
console.log(commands.map(command => ` ${command}`).join('\n'));
|
|
215
|
+
const followUp = stripIndent`
|
|
216
|
+
Quilt can also help you build, test, lint, and type-check your new package.
|
|
217
|
+
You can learn more about building packages with Quilt by reading the documentation:
|
|
218
|
+
${underline_1(magenta_1('https://github.com/lemonmade/quilt/tree/main/documentation'))}
|
|
219
|
+
|
|
220
|
+
Have fun! 🎉
|
|
221
|
+
`;
|
|
222
|
+
console.log();
|
|
223
|
+
console.log(followUp);
|
|
224
|
+
} // Argument handling
|
|
225
|
+
|
|
226
|
+
function getArgv() {
|
|
227
|
+
const argv = arg_1({
|
|
228
|
+
'--yes': Boolean,
|
|
229
|
+
'-y': '--yes',
|
|
230
|
+
'--name': String,
|
|
231
|
+
'--directory': String,
|
|
232
|
+
'--install': Boolean,
|
|
233
|
+
'--no-install': Boolean,
|
|
234
|
+
'--monorepo': Boolean,
|
|
235
|
+
'--no-monorepo': Boolean,
|
|
236
|
+
'--package-manager': String,
|
|
237
|
+
'--extras': [String],
|
|
238
|
+
'--no-extras': Boolean,
|
|
239
|
+
'--react': Boolean,
|
|
240
|
+
'--no-react': Boolean,
|
|
241
|
+
'--public': Boolean,
|
|
242
|
+
'--private': Boolean,
|
|
243
|
+
'--registry': String,
|
|
244
|
+
'--help': Boolean,
|
|
245
|
+
'-h': '--help'
|
|
246
|
+
}, {
|
|
247
|
+
permissive: true
|
|
248
|
+
});
|
|
249
|
+
return argv;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
async function getName(argv) {
|
|
253
|
+
let {
|
|
254
|
+
'--name': name
|
|
255
|
+
} = argv;
|
|
256
|
+
|
|
257
|
+
if (name == null) {
|
|
258
|
+
name = await prompt({
|
|
259
|
+
type: 'text',
|
|
260
|
+
message: 'What would you like to name your new package?',
|
|
261
|
+
initial: '@my-team/package'
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return name;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async function getDirectory(argv, {
|
|
269
|
+
name,
|
|
270
|
+
inWorkspace
|
|
271
|
+
}) {
|
|
272
|
+
let directory = argv['--directory'] ? path.resolve(argv['--directory']) : undefined;
|
|
273
|
+
|
|
274
|
+
if (directory == null) {
|
|
275
|
+
const basePackageName = toValidPackageName(name.split('/').pop());
|
|
276
|
+
const defaultDirectory = inWorkspace ? `packages/${basePackageName}` : basePackageName;
|
|
277
|
+
directory = path.resolve(await prompt({
|
|
278
|
+
type: 'text',
|
|
279
|
+
message: 'Where would you like to create your new package?',
|
|
280
|
+
initial: defaultDirectory
|
|
281
|
+
}));
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
while (!argv['--yes']) {
|
|
285
|
+
if (fs.existsSync(directory) && !(await isEmpty(directory))) {
|
|
286
|
+
const relativeDirectory = path.relative(process.cwd(), directory);
|
|
287
|
+
const empty = await prompt({
|
|
288
|
+
type: 'confirm',
|
|
289
|
+
message: `Directory ${bold_1(relativeDirectoryForDisplay(relativeDirectory))} is not empty, is it safe to empty it?`,
|
|
290
|
+
initial: true
|
|
291
|
+
});
|
|
292
|
+
if (empty) break;
|
|
293
|
+
const promptDirectory = await prompt({
|
|
294
|
+
type: 'text',
|
|
295
|
+
message: 'What directory do you want to create your package in?'
|
|
296
|
+
});
|
|
297
|
+
directory = path.resolve(promptDirectory);
|
|
298
|
+
} else {
|
|
299
|
+
break;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
return directory;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
async function getPublic(argv) {
|
|
307
|
+
let isPublic;
|
|
308
|
+
|
|
309
|
+
if (argv['--public'] || argv['--yes']) {
|
|
310
|
+
isPublic = true;
|
|
311
|
+
} else if (argv['--private']) {
|
|
312
|
+
isPublic = false;
|
|
313
|
+
} else {
|
|
314
|
+
isPublic = await prompt({
|
|
315
|
+
type: 'confirm',
|
|
316
|
+
message: 'Will you publish this package to use in other projects?',
|
|
317
|
+
initial: true
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
return isPublic;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
async function getReact(argv) {
|
|
325
|
+
let useReact;
|
|
326
|
+
|
|
327
|
+
if (argv['--react'] || argv['--yes']) {
|
|
328
|
+
useReact = true;
|
|
329
|
+
} else if (argv['--no-react']) {
|
|
330
|
+
useReact = false;
|
|
331
|
+
} else {
|
|
332
|
+
useReact = await prompt({
|
|
333
|
+
type: 'confirm',
|
|
334
|
+
message: 'Will this package depend on React?',
|
|
335
|
+
initial: true
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return useReact;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function adjustPackageJson(packageJson, {
|
|
343
|
+
name,
|
|
344
|
+
react,
|
|
345
|
+
isPublic,
|
|
346
|
+
registry
|
|
347
|
+
}) {
|
|
348
|
+
packageJson.name = name;
|
|
349
|
+
const packageParts = name.split('/');
|
|
350
|
+
const scope = packageParts[0].startsWith('@') ? packageParts[0] : undefined;
|
|
351
|
+
const finalRegistry = registry ?? 'https://registry.npmjs.org';
|
|
352
|
+
|
|
353
|
+
if (scope) {
|
|
354
|
+
packageJson.publishConfig[`${scope}/registry`] = finalRegistry;
|
|
355
|
+
} else if (registry) {
|
|
356
|
+
packageJson.publishConfig.registry = finalRegistry;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if (isPublic) {
|
|
360
|
+
delete packageJson.private;
|
|
361
|
+
} else {
|
|
362
|
+
delete packageJson.publishConfig;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
if (!react) {
|
|
366
|
+
delete packageJson.dependencies['@types/react'];
|
|
367
|
+
delete packageJson.devDependencies['react'];
|
|
368
|
+
delete packageJson.peerDependencies['react'];
|
|
369
|
+
delete packageJson.peerDependenciesMeta['react'];
|
|
370
|
+
packageJson.eslintConfig.extends = packageJson.eslintConfig.extends.filter(extend => !extend.includes('react'));
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
return packageJson;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
function sortKeys(object) {
|
|
377
|
+
const newObject = {};
|
|
378
|
+
const sortedEntries = Object.entries(object).sort(([keyOne], [keyTwo]) => keyOne.localeCompare(keyTwo));
|
|
379
|
+
|
|
380
|
+
for (const [key, value] of sortedEntries) {
|
|
381
|
+
newObject[key] = value;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
return newObject;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
export { createPackage };
|