@quilted/create 0.1.20 → 0.1.23

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.
@@ -0,0 +1,315 @@
1
+ 'use strict';
2
+
3
+ var path = require('path');
4
+ var fs = require('fs');
5
+ var url = require('url');
6
+ require('./index.cjs');
7
+
8
+ function _interopNamespace(e) {
9
+ if (e && e.__esModule) return e;
10
+ var n = Object.create(null);
11
+ if (e) {
12
+ Object.keys(e).forEach(function (k) {
13
+ if (k !== 'default') {
14
+ var d = Object.getOwnPropertyDescriptor(e, k);
15
+ Object.defineProperty(n, k, d.get ? d : {
16
+ enumerable: true,
17
+ get: function () { return e[k]; }
18
+ });
19
+ }
20
+ });
21
+ }
22
+ n["default"] = e;
23
+ return Object.freeze(n);
24
+ }
25
+
26
+ var path__namespace = /*#__PURE__*/_interopNamespace(path);
27
+ var fs__namespace = /*#__PURE__*/_interopNamespace(fs);
28
+
29
+ function loadTemplate(name) {
30
+ let templateRootPromise;
31
+ return {
32
+ async copy(to, handleFile) {
33
+ var _templateRootPromise;
34
+
35
+ (_templateRootPromise = templateRootPromise) !== null && _templateRootPromise !== void 0 ? _templateRootPromise : templateRootPromise = templateDirectory(name);
36
+ const templateRoot = await templateRootPromise;
37
+ const targetRoot = path__namespace.resolve(to);
38
+ const files = fs__namespace.readdirSync(templateRoot).filter(file => !path__namespace.basename(file).startsWith('.'));
39
+
40
+ for (const file of files) {
41
+ if (handleFile) {
42
+ if (!handleFile(file)) {
43
+ continue;
44
+ }
45
+ }
46
+
47
+ const targetPath = path__namespace.join(targetRoot, file.startsWith('_') ? `.${file.slice(1)}` : file);
48
+ copy(path__namespace.join(templateRoot, file), targetPath);
49
+ }
50
+ },
51
+
52
+ async read(file) {
53
+ var _templateRootPromise2;
54
+
55
+ (_templateRootPromise2 = templateRootPromise) !== null && _templateRootPromise2 !== void 0 ? _templateRootPromise2 : templateRootPromise = templateDirectory(name);
56
+ const templateRoot = await templateRootPromise;
57
+ return fs__namespace.readFileSync(path__namespace.join(templateRoot, file), {
58
+ encoding: 'utf8'
59
+ });
60
+ }
61
+
62
+ };
63
+ }
64
+ function createOutputTarget(target) {
65
+ return {
66
+ root: target,
67
+
68
+ read(file) {
69
+ return fs__namespace.promises.readFile(path__namespace.resolve(target, file), {
70
+ encoding: 'utf8'
71
+ });
72
+ },
73
+
74
+ async write(file, content) {
75
+ await writeFile(path__namespace.resolve(target, file), content);
76
+ }
77
+
78
+ };
79
+ }
80
+ let packageRootPromise;
81
+
82
+ async function templateDirectory(name) {
83
+ return path__namespace.join(await getPackageRoot(), 'templates', name);
84
+ }
85
+
86
+ async function getPackageRoot() {
87
+ if (!packageRootPromise) {
88
+ packageRootPromise = (async () => {
89
+ const {
90
+ packageDirectory
91
+ } = await Promise.resolve().then(function () { return require('./index2.cjs'); });
92
+ return packageDirectory({
93
+ cwd: path__namespace.dirname(url.fileURLToPath((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('package-manager.cjs', document.baseURI).href))))
94
+ });
95
+ })();
96
+ }
97
+
98
+ return packageRootPromise;
99
+ }
100
+
101
+ function toValidPackageName(projectName) {
102
+ return projectName.trim().toLowerCase().replace(/\s+/g, '-').replace(/^[._]/, '').replace(/[^a-z0-9-~@/]+/g, '-');
103
+ }
104
+
105
+ function copy(source, destination) {
106
+ const stat = fs__namespace.statSync(source);
107
+
108
+ if (stat.isDirectory()) {
109
+ copyDirectory(source, destination);
110
+ } else {
111
+ fs__namespace.copyFileSync(source, destination);
112
+ }
113
+ }
114
+
115
+ async function copyDirectory(source, destination) {
116
+ fs__namespace.mkdirSync(destination, {
117
+ recursive: true
118
+ });
119
+
120
+ for (const file of fs__namespace.readdirSync(source)) {
121
+ const srcFile = path__namespace.resolve(source, file);
122
+ const destFile = path__namespace.resolve(destination, file);
123
+ copy(srcFile, destFile);
124
+ }
125
+ }
126
+
127
+ async function writeFile(file, content) {
128
+ await fs__namespace.promises.writeFile(file, content);
129
+ }
130
+ async function isEmpty(path) {
131
+ return fs__namespace.readdirSync(path).length === 0;
132
+ }
133
+ async function emptyDirectory(dir) {
134
+ if (!fs__namespace.existsSync(dir)) {
135
+ return;
136
+ }
137
+
138
+ for (const file of fs__namespace.readdirSync(dir)) {
139
+ fs__namespace.rmSync(path__namespace.resolve(dir, file), {
140
+ force: true,
141
+ recursive: true
142
+ });
143
+ }
144
+ }
145
+ function relativeDirectoryForDisplay(relativeDirectory) {
146
+ return relativeDirectory.startsWith('.') ? relativeDirectory : `.${path__namespace.sep}${relativeDirectory}`;
147
+ }
148
+ async function format(content, {
149
+ as: parser
150
+ }) {
151
+ const [{
152
+ format
153
+ }, {
154
+ default: babel
155
+ }, {
156
+ default: yaml
157
+ }] = await Promise.all([Promise.resolve().then(function () { return require('./standalone.cjs'); }).then(function (n) { return n.standalone; }), Promise.resolve().then(function () { return require('./parser-babel.cjs'); }).then(function (n) { return n.parserBabel; }), Promise.resolve().then(function () { return require('./parser-yaml.cjs'); }).then(function (n) { return n.parserYaml; })]);
158
+ return format(content, {
159
+ arrowParens: 'always',
160
+ bracketSpacing: false,
161
+ singleQuote: true,
162
+ trailingComma: 'all',
163
+ parser,
164
+ plugins: [babel, yaml]
165
+ });
166
+ }
167
+
168
+ const ENDS_WITH_TSCONFIG = /[/]?tsconfig[.a-z0-9]*[.]json/i;
169
+ async function addToTsConfig(directory, output) {
170
+ var _tsconfig$references;
171
+
172
+ const tsconfig = JSON.parse(await output.read('tsconfig.json'));
173
+ (_tsconfig$references = tsconfig.references) !== null && _tsconfig$references !== void 0 ? _tsconfig$references : tsconfig.references = [];
174
+ const relativePath = path.relative(output.root, directory);
175
+ const relativeForDisplay = relativeDirectoryForDisplay(relativePath);
176
+
177
+ if (tsconfig.references.length === 0) {
178
+ tsconfig.references.push({
179
+ path: relativeForDisplay
180
+ });
181
+ } else {
182
+ let hasExistingReference = false;
183
+ let referenceFormat = 'pretty-relative';
184
+
185
+ for (const {
186
+ path
187
+ } of tsconfig.references) {
188
+ if (path.startsWith('./')) {
189
+ if (ENDS_WITH_TSCONFIG.test(path)) {
190
+ referenceFormat = 'pretty-tsconfig';
191
+
192
+ if (path === `${relativeForDisplay}/tsconfig.json`) {
193
+ hasExistingReference = true;
194
+ break;
195
+ }
196
+ }
197
+
198
+ referenceFormat = 'pretty-relative';
199
+
200
+ if (path === relativeForDisplay) {
201
+ hasExistingReference = true;
202
+ break;
203
+ }
204
+ } else if (ENDS_WITH_TSCONFIG.test(path)) {
205
+ referenceFormat = 'tsconfig';
206
+
207
+ if (path === `${relativePath}/tsconfig.json`) {
208
+ hasExistingReference = true;
209
+ break;
210
+ }
211
+ } else {
212
+ referenceFormat = 'relative';
213
+
214
+ if (path === relativePath) {
215
+ hasExistingReference = true;
216
+ break;
217
+ }
218
+ }
219
+ }
220
+
221
+ if (!hasExistingReference) {
222
+ let path;
223
+
224
+ if (referenceFormat === 'pretty-tsconfig') {
225
+ path = `${relativeForDisplay}/tsconfig.json`;
226
+ } else if (referenceFormat === 'pretty-relative') {
227
+ path = relativeForDisplay;
228
+ } else if (referenceFormat === 'tsconfig') {
229
+ path = `${relativePath}/tsconfig.json`;
230
+ } else {
231
+ path = relativePath;
232
+ }
233
+
234
+ tsconfig.references.push({
235
+ path
236
+ });
237
+ }
238
+ }
239
+
240
+ tsconfig.references = tsconfig.references.sort(({
241
+ path: pathOne
242
+ }, {
243
+ path: pathTwo
244
+ }) => pathOne.localeCompare(pathTwo));
245
+ await output.write('tsconfig.json', await format(JSON.stringify(tsconfig), {
246
+ as: 'json'
247
+ }));
248
+ }
249
+
250
+ async function addToPackageManagerWorkspaces(directory, output, packageManager) {
251
+ if (packageManager === 'pnpm') {
252
+ var _workspaceYaml$packag;
253
+
254
+ const {
255
+ parse,
256
+ stringify
257
+ } = await Promise.resolve().then(function () { return require('./index3.cjs'); }).then(function (n) { return n.index; });
258
+ const workspaceYaml = parse(await output.read('pnpm-workspace.yaml'));
259
+ workspaceYaml.packages = await addToWorkspaces(path.relative(output.root, directory), (_workspaceYaml$packag = workspaceYaml.packages) !== null && _workspaceYaml$packag !== void 0 ? _workspaceYaml$packag : []);
260
+ await output.write('pnpm-workspace.yaml', await format(stringify(workspaceYaml), {
261
+ as: 'yaml'
262
+ }));
263
+ } else {
264
+ var _packageJson$workspac;
265
+
266
+ const packageJson = JSON.parse(await output.read('package.json'));
267
+ packageJson.workspaces = await addToWorkspaces(path.relative(output.root, directory), (_packageJson$workspac = packageJson.workspaces) !== null && _packageJson$workspac !== void 0 ? _packageJson$workspac : []);
268
+ await output.write('package.json', await format(JSON.stringify(packageJson), {
269
+ as: 'json-stringify'
270
+ }));
271
+ }
272
+ }
273
+
274
+ async function addToWorkspaces(relative, workspaces) {
275
+ if (workspaces.length === 0) {
276
+ return [relative];
277
+ } // Default documentation seems to generally exclude leading `./` on paths
278
+
279
+
280
+ let pretty = false;
281
+ let hasMatch = false;
282
+ const {
283
+ default: minimatch
284
+ } = await Promise.resolve().then(function () { return require('./minimatch.cjs'); }).then(function (n) { return n.minimatch; });
285
+
286
+ for (const pattern of workspaces) {
287
+ let normalizedPattern = pattern;
288
+
289
+ if (pattern.startsWith('./')) {
290
+ pretty = true;
291
+ normalizedPattern = pattern.slice(2);
292
+ }
293
+
294
+ if (minimatch(relative, normalizedPattern)) {
295
+ hasMatch = true;
296
+ break;
297
+ }
298
+ }
299
+
300
+ if (hasMatch) {
301
+ return workspaces;
302
+ }
303
+
304
+ return [...workspaces, pretty ? relativeDirectoryForDisplay(relative) : relative].sort((patternOne, patternTwo) => patternOne.localeCompare(patternTwo));
305
+ }
306
+
307
+ exports.addToPackageManagerWorkspaces = addToPackageManagerWorkspaces;
308
+ exports.addToTsConfig = addToTsConfig;
309
+ exports.createOutputTarget = createOutputTarget;
310
+ exports.emptyDirectory = emptyDirectory;
311
+ exports.format = format;
312
+ exports.isEmpty = isEmpty;
313
+ exports.loadTemplate = loadTemplate;
314
+ exports.relativeDirectoryForDisplay = relativeDirectoryForDisplay;
315
+ exports.toValidPackageName = toValidPackageName;