create-gasket-app 7.0.1-cli.0 → 7.0.1

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.
Files changed (35) hide show
  1. package/README.md +223 -3
  2. package/lib/commands/create.js +149 -0
  3. package/lib/config/default-plugins.js +15 -0
  4. package/lib/index.d.ts +309 -0
  5. package/lib/index.js +24 -9
  6. package/lib/scaffold/action-wrapper.js +37 -0
  7. package/lib/scaffold/actions/create-hooks.js +27 -0
  8. package/lib/scaffold/actions/generate-files.js +189 -0
  9. package/lib/scaffold/actions/global-prompts.js +153 -0
  10. package/lib/scaffold/actions/index.js +33 -0
  11. package/lib/scaffold/actions/install-modules.js +15 -0
  12. package/lib/scaffold/actions/link-modules.js +19 -0
  13. package/lib/scaffold/actions/load-preset.js +60 -0
  14. package/lib/scaffold/actions/mkdir.js +30 -0
  15. package/lib/scaffold/actions/post-create-hooks.js +31 -0
  16. package/lib/scaffold/actions/preset-config-hooks.js +13 -0
  17. package/lib/scaffold/actions/preset-prompt-hooks.js +17 -0
  18. package/lib/scaffold/actions/print-report.js +89 -0
  19. package/lib/scaffold/actions/prompt-hooks.js +43 -0
  20. package/lib/scaffold/actions/setup-pkg.js +34 -0
  21. package/lib/scaffold/actions/write-gasket-config.js +119 -0
  22. package/lib/scaffold/actions/write-pkg.js +19 -0
  23. package/lib/scaffold/config-builder.js +526 -0
  24. package/lib/scaffold/create-context.js +188 -0
  25. package/lib/scaffold/dump-error-context.js +28 -0
  26. package/lib/scaffold/files.js +30 -0
  27. package/lib/scaffold/readme.js +51 -0
  28. package/lib/scaffold/utils.js +22 -0
  29. package/lib/utils/create-option.js +18 -0
  30. package/lib/utils/index.js +5 -0
  31. package/lib/utils/logo.js +12 -0
  32. package/lib/utils/process-args.js +37 -0
  33. package/lib/utils/process-command.js +50 -0
  34. package/lib/utils/process-options.js +44 -0
  35. package/package.json +36 -9
package/lib/index.d.ts ADDED
@@ -0,0 +1,309 @@
1
+ import type { GasketConfigDefinition, MaybeAsync, Plugin, GasketEngine } from '@gasket/core';
2
+ import type { PackageManager } from '@gasket/utils';
3
+ import type { PromptModule } from 'inquirer';
4
+
5
+ export interface Dependencies {
6
+ dependencies?: Record<string, string>;
7
+ devDependencies?: Record<string, string>;
8
+ peerDependencies?: Record<string, string>;
9
+ }
10
+
11
+ export interface PackageJson extends Dependencies {
12
+ name: string;
13
+ version?: string;
14
+ description?: string;
15
+ license?: string;
16
+ repository?:
17
+ | string
18
+ | {
19
+ type: 'git';
20
+ url: string;
21
+ };
22
+ scripts?: Record<string, string>;
23
+ optionalDependencies?: Record<string, string>;
24
+ homepage?: string;
25
+ }
26
+
27
+ export interface ModuleInfo {
28
+ name: string;
29
+ module: any;
30
+ path?: string;
31
+ package?: PackageJson;
32
+ version?: string;
33
+ }
34
+
35
+ export interface PresetInfo extends ModuleInfo { }
36
+
37
+ export interface PluginInfo extends ModuleInfo { }
38
+
39
+ export interface ConfigBuilder<Config> {
40
+ /**
41
+ * fields object
42
+ */
43
+ fields: { [key: string]: any };
44
+
45
+ /**
46
+ * Adds all `[key, value]` pairs in the `fields` provided.
47
+ * @param fields - Object to merge. Can be a function that accepts the current fields and object to merge.
48
+ */
49
+ extend(
50
+ fields: Partial<Config> | ((current: Config) => Partial<Config>)
51
+ ): void;
52
+
53
+ /**
54
+ * Performs an intelligent, domain-aware merge of the `value` for
55
+ * the given `key` into the package.json fields associated with this instance.
56
+ *
57
+ * @param key - Field in package.json to add or extend.
58
+ * @param value - Target value to set for key provided.
59
+ * @param [options] - Optional arguments for add behavior
60
+ * @param [options.force] - Should the semver version override other attempts
61
+ *
62
+ * Adapted from @vue/cli under MIT License:
63
+ * https://github.com/vuejs/vue-cli/blob/f09722c/packages/%40vue/cli/lib/GeneratorAPI.js#L117-L150
64
+ */
65
+ add<Key extends keyof Config>(
66
+ key: Key,
67
+ value: Config[Key],
68
+ options?: { force?: boolean }
69
+ ): void;
70
+
71
+ /**
72
+ * addPlugin - Add plugin import to the gasket file and use the value in the plugins array
73
+ * @param {string} pluginImport - name of the import used as a value - `import pluginImport...`
74
+ * @param {string} pluginName - name of the plugin import/package - `from 'pluginName'`
75
+ * @example
76
+ * addPlugin('pluginA', '@gasket/plugin-a')
77
+ *
78
+ * // gasket.js
79
+ * import pluginA from '@gasket/plugin-a';
80
+ *
81
+ * export default makeGasket({
82
+ * plugins: [
83
+ * pluginA
84
+ * ]
85
+ * });
86
+ */
87
+ addPlugin(pluginImport: string, pluginName: string): void;
88
+
89
+ /**
90
+ * addImport - Add a non-plugin import to the gasket file
91
+ * @param {string} importName - name of the import used as a value - `import fs...`
92
+ * @param {string} importPath - path of the import - `from 'fs'`
93
+ * @returns {ConfigBuilder} - instance for chaining
94
+ * @example
95
+ * Can be default or named import
96
+ * addImport('{ readFileSync }', 'fs')
97
+ * addImport('fs', 'fs')
98
+ *
99
+ * // gasket.js
100
+ * import { readFileSync } from 'fs';
101
+ * import fs from 'fs';
102
+ */
103
+ addImport(importName: string, importPath: string): ConfigBuilder<Config>;
104
+
105
+ /**
106
+ * addExpression - add programmatic expression to the gasket file
107
+ * @param {string} expression - expression to add after imports
108
+ * @returns {ConfigBuilder} - instance for chaining
109
+ * @example
110
+ *
111
+ * .addImport('fs', 'fs')
112
+ * .addExpression('const file = fs.readFileSync(\'./file.txt\')')
113
+ *
114
+ * // gasket.js
115
+ * import fs from 'fs';
116
+ * const file = fs.readFileSync('./file.txt');
117
+ */
118
+ addExpression(expression: string): ConfigBuilder<Config>;
119
+
120
+ /**
121
+ * injectValue - Inject a value into the gasket config object
122
+ * @param {string} configKey - collapsed object path to inject value into - `express.config.routes`
123
+ * @param {string} injectedValue - string used as a value
124
+ * @example
125
+ * .addImport('{ routes }', './routes')
126
+ * .injectValue('express.routes', 'routes');
127
+ *
128
+ * // gasket.js
129
+ * export default makeGasket({
130
+ * express: {
131
+ * routes: routes
132
+ * }
133
+ * });
134
+ */
135
+ injectValue(configKey: string, injectedValue: string): void;
136
+ }
137
+
138
+ export interface PackageJsonBuilder extends ConfigBuilder<PackageJson> {
139
+ /**
140
+ * Checks if a property exists on the package.json fields.
141
+ * @param key - Field to search
142
+ * @param value - Value to search for
143
+ * @returns True if the property exists and matches the value
144
+ */
145
+ has(key: keyof PackageJson, value: string): boolean;
146
+ }
147
+
148
+ export interface Files {
149
+ add(...args: string[]): void;
150
+ }
151
+
152
+ export interface Readme {
153
+ /** Markdown content to be injected into the app readme */
154
+ markdown: string[];
155
+
156
+ /** Links to be added to the footer */
157
+ links: string[];
158
+
159
+ /** Add a markdown heading */
160
+ heading(content: string, level?: number): Readme;
161
+
162
+ /** Add a markdown sub-heading */
163
+ subHeading(content: string): Readme;
164
+
165
+ /** Add markdown content */
166
+ content(markdown: string);
167
+
168
+ /** Add a markdown list */
169
+ list(items: string[]): Readme;
170
+
171
+ /** Add a markdown link - printed in footer */
172
+ link(content: string, href: string): Readme;
173
+
174
+ /** Add a markdown code block */
175
+ codeBlock(content: string, syntax?: string): Readme;
176
+
177
+ /** Add markdown file */
178
+ markdownFile(path: string): Promise<Readme>;
179
+ }
180
+
181
+ export type CreatePrompt = PromptModule;
182
+
183
+ declare module 'create-gasket-app' {
184
+ export interface CreateContext {
185
+ /** Short name of the app */
186
+ appName: string;
187
+
188
+ /** Current work directory */
189
+ cwd: string;
190
+
191
+ /** Path to the target app (Default: cwd/appName) */
192
+ dest: string;
193
+
194
+ /** Relative path to the target app */
195
+ relDest: string;
196
+
197
+ /** Whether or not target directory already exists */
198
+ extant: boolean;
199
+
200
+ /** paths to the local presets packages */
201
+ localPresets: Array<string>;
202
+
203
+ /** Raw preset desc from args. Can include version constraint. Added by
204
+ * load-preset if using localPresets. */
205
+ rawPresets: Array<string>;
206
+
207
+ /** Local packages that should be linked */
208
+ pkgLinks: Array<string>;
209
+
210
+ /** non-error/warning messages to report */
211
+ messages: Array<string>;
212
+
213
+ /** warnings messages to report */
214
+ warnings: Array<string>;
215
+
216
+ /** error messages to report but do not exit process */
217
+ errors: Array<string>;
218
+
219
+ /** any next steps to report for user */
220
+ nextSteps: Array<string>;
221
+
222
+ /** any generated files to show in report */
223
+ generatedFiles: Set<string>;
224
+
225
+ /** Default empty array, populated by load-preset with actual imports */
226
+ presets: Array<Plugin>;
227
+
228
+ /** Default to object w/empty plugins array to be populated by `presetConfig` hook */
229
+ presetConfig: GasketConfigDefinition;
230
+
231
+ // Added by `global-prompts`
232
+
233
+ /** Description of app */
234
+ appDescription: string;
235
+
236
+ /** Should a git repo be initialized and first commit */
237
+ gitInit: boolean;
238
+
239
+ /** Names of the plugins that add unit and integration tests */
240
+ testPlugins: Array<string>;
241
+
242
+ /** Which package manager to use (Default: 'npm') */
243
+ packageManager: string;
244
+
245
+ /** Derived install command (Default: 'npm install') */
246
+ installCmd: string;
247
+
248
+ /** Derived local run command (Default: 'npx gasket local') */
249
+ localCmd: string;
250
+
251
+ /** Whether or not the user wants to override an extant directory */
252
+ destOverride: boolean;
253
+
254
+ // Added by `setup-pkg`
255
+
256
+ /** package.json builder */
257
+ pkg: PackageJsonBuilder;
258
+
259
+ /** manager to execute npm or yarn commands */
260
+ pkgManager: PackageManager;
261
+
262
+ // Added by `setup-gasket-config`
263
+
264
+ /** gasket.config builder */
265
+ gasketConfig: ConfigBuilder<GasketConfigDefinition>;
266
+
267
+ // Added by `create-hooks`
268
+
269
+ /** Use to add files and templates to generate */
270
+ files: Files;
271
+
272
+ /** Use to add content to the README.md */
273
+ readme: Readme;
274
+ }
275
+ }
276
+
277
+ export interface ActionWrapperParams {
278
+ gasket: GasketEngine;
279
+ context: CreateContext;
280
+ spinner?: any;
281
+ }
282
+
283
+ declare module '@gasket/core' {
284
+
285
+ export interface HookExecTypes {
286
+ presetPrompt(
287
+ context: CreateContext,
288
+ utils: {
289
+ prompt: CreatePrompt;
290
+ }
291
+ ): Promise<void>;
292
+ presetConfig(context: CreateContext): Promise<CreateContext['presetConfig']>;
293
+ prompt(
294
+ context: CreateContext,
295
+ utils: {
296
+ prompt: CreatePrompt;
297
+ }
298
+ ): MaybeAsync<CreateContext>;
299
+
300
+ create(context: CreateContext): MaybeAsync<void>;
301
+
302
+ postCreate(
303
+ context: CreateContext,
304
+ utils: {
305
+ runScript: (script: string) => Promise<void>;
306
+ }
307
+ ): MaybeAsync<void>;
308
+ }
309
+ }
package/lib/index.js CHANGED
@@ -1,16 +1,31 @@
1
1
  #!/usr/bin/env node
2
+ import { Command } from 'commander';
3
+ import { createRequire } from 'module';
4
+ import { createCommand } from './commands/create.js';
5
+ import { processCommand, logo } from './utils/index.js';
6
+ import { warnIfOutdated } from '@gasket/utils';
2
7
 
3
- const { fork } = require('child_process');
4
- const path = require('path');
8
+ const require = createRequire(import.meta.url);
9
+ const pkg = require('../package.json');
10
+ const program = new Command();
5
11
 
6
- const [, , ...args] = process.argv;
12
+ // Create Gasket CLI
13
+ const gasketBin = program
14
+ .name('create-gasket-app')
15
+ .description(pkg.description)
16
+ .option('--gasket-config [gasket-config-path]', 'Fully qualified Gasket config to load', 'gasket.config')
17
+ .version(pkg.version)
18
+ .addHelpText('beforeAll', logo);
7
19
 
8
- function main() {
9
- return fork(
10
- path.join(__dirname, '..', 'node_modules', '.bin', 'gasket'),
11
- ['create', ...args],
12
- { stdio: 'inherit', stdin: 'inherit', stderr: 'inherit' }
13
- );
20
+
21
+ async function main() {
22
+ const { command, hidden, isDefault } = processCommand(createCommand);
23
+ await warnIfOutdated(pkg.name, pkg.version);
24
+ gasketBin.addCommand(command, { hidden, isDefault });
25
+
26
+ process.argv.splice(2, 0, 'create');
27
+
28
+ return await gasketBin.parseAsync();
14
29
  }
15
30
 
16
31
  main();
@@ -0,0 +1,37 @@
1
+ import ora from 'ora';
2
+
3
+ /**
4
+ * Decorate a create action with spinner.
5
+ * If an action throws, a fail spinner will render for the step, regardless of
6
+ * if the spinner was started or not.
7
+ *
8
+ * @param {String} label - Label for the spinner
9
+ * @param {Function} fn - Action to wrap
10
+ * @param {Boolean} startSpinner - Should the spinner start
11
+ * @returns {Function} decorated action
12
+ */
13
+ export default function withSpinner(label, fn, { startSpinner = true } = {}) {
14
+ /**
15
+ * Decorated function
16
+ *
17
+ * @param {ActionWrapperParams} params - ActionWrapperParams
18
+ * @param {GasketEngine} params.gasket - Gasket config
19
+ * @param {CreateContext} params.context - Create context
20
+ * @returns {Promise} promise
21
+ */
22
+ async function wrapper({ gasket = {}, context }) {
23
+ const spinner = ora(label);
24
+ if (startSpinner) spinner.start();
25
+ try {
26
+ await fn({ gasket, context, spinner });
27
+ if (spinner.isSpinning) spinner.succeed();
28
+ } catch (err) {
29
+ spinner.fail();
30
+ context.errors.push(err.stack);
31
+ throw err;
32
+ }
33
+ }
34
+
35
+ wrapper.wrapped = fn;
36
+ return wrapper;
37
+ }
@@ -0,0 +1,27 @@
1
+ import action from '../action-wrapper.js';
2
+ import { ConfigBuilder } from '../config-builder.js';
3
+ import { Files } from '../files.js';
4
+ import Readme from '../readme.js';
5
+
6
+ /**
7
+ * Executes the `create` hook for all registered plugins.
8
+ * Adds `files` to context for plugins to add their files and templates.
9
+ *
10
+ * @param {GasketEngine} gasket - Gasket API
11
+ * @param {CreateContext} context - Create context
12
+ * @returns {Promise} promise
13
+ */
14
+ async function createHooks({ gasket, context }) {
15
+ const { plugins = [], warnings } = context;
16
+
17
+ const files = new Files();
18
+ const readme = new Readme();
19
+ const gasketConfig = ConfigBuilder.create({}, { orderBy: ['plugins'], warnings, plugins });
20
+ Object.assign(context, { files, readme, gasketConfig });
21
+
22
+ await gasket.execApply('create', async function applyCreate(plugin, handler) {
23
+ await handler(context.runWith(plugin));
24
+ });
25
+ }
26
+
27
+ export default action('Execute create hooks', createHooks);
@@ -0,0 +1,189 @@
1
+ import { default as diagnostics } from 'diagnostics';
2
+ const debug = diagnostics('gasket:cli:generate-files');
3
+ import fs from 'fs/promises';
4
+ import { promisify } from 'util';
5
+ import Handlebars from 'handlebars';
6
+ import path from 'path';
7
+ import mkdirp from 'mkdirp';
8
+ import chalk from 'chalk';
9
+ const { dim } = chalk;
10
+ import action from '../action-wrapper.js';
11
+ import * as syncGlob from 'glob';
12
+ const glob = promisify(syncGlob.default);
13
+
14
+ const flatten = (acc, values) => (acc || []).concat(values);
15
+ const reSep = /[/\\]+/;
16
+ const joinSep = pthArr => pthArr.join(path.sep);
17
+ const splitSep = pthStr => pthStr.split(reSep);
18
+
19
+ /**
20
+ * Find all duplicate target files and reduce to single descriptor.
21
+ * Keeps track of overrides.
22
+ * Last in wins.
23
+ *
24
+ * @param {object[]} descriptors - Details about files to generate
25
+ * @returns {object[]} descriptors
26
+ */
27
+ function reduceDescriptors(descriptors) {
28
+ const reduced = descriptors.reduce((acc, desc) => {
29
+ const { target } = desc;
30
+ if (acc.has(target)) {
31
+ const prev = acc.get(target);
32
+ if (prev.from !== desc.from) {
33
+ debug(`Using "${target}" from ${desc.from} instead of ${prev.from}`);
34
+ desc.overrides = prev.from;
35
+ }
36
+ }
37
+ acc.set(target, desc);
38
+ return acc;
39
+ }, new Map());
40
+
41
+ return Array.from(reduced.values());
42
+ }
43
+
44
+ /**
45
+ * Assemble the description objects from glob results
46
+ *
47
+ * @param {string} dest - app destination
48
+ * @param {string} from - Plugin source name
49
+ * @param {string} pattern - pattern to glob
50
+ * @param {string[]} srcPaths - resulting paths from glob
51
+ * @returns {object[]} descriptors
52
+ */
53
+ function assembleDescriptors(dest, from, pattern, srcPaths) {
54
+ const output = joinSep(splitSep(dest));
55
+ const baseParts = splitSep(path.resolve(pattern.replace(/[/\\]+\.?\*.*$/, '')));
56
+ const base = joinSep(baseParts);
57
+ return srcPaths.map((srcPath) => {
58
+ const parts = splitSep(srcPath);
59
+ const srcFile = joinSep(parts);
60
+ const target = joinSep(parts.slice(baseParts.length)).replace('.template', '');
61
+ return {
62
+ pattern,
63
+ base,
64
+ srcFile,
65
+ targetFile: joinSep([output, target]),
66
+ target,
67
+ from
68
+ };
69
+ });
70
+ }
71
+
72
+ /**
73
+ * Build a list of descriptions of all files we want to generate
74
+ *
75
+ * @param {CreateContext} context - Create context
76
+ * @returns {object[]} descriptors
77
+ */
78
+ async function getDescriptors(context) {
79
+ const { dest, files } = context;
80
+ const descriptors = (
81
+ await Promise.all(files.globSets.map(async set => {
82
+ const { globs, source } = set;
83
+ const matches = await Promise.all(globs.map(async pattern => {
84
+ pattern = splitSep(pattern).join('/'); // Glob uses / for all OS's
85
+ const srcPaths = await glob(pattern, { nodir: true });
86
+ return assembleDescriptors(dest, source.name, pattern, srcPaths);
87
+ }));
88
+ return matches.reduce(flatten);
89
+ }))
90
+ ).reduce(flatten);
91
+
92
+ return reduceDescriptors(descriptors);
93
+ }
94
+
95
+ /**
96
+ * Read file content, apply templating, then write out target file.
97
+ *
98
+ * @param {CreateContext} context - Create context
99
+ * @param {object[]} descriptors - Details about files to generate
100
+ * @returns {boolean} hasWarnings
101
+ */
102
+ async function performGenerate(context, descriptors) {
103
+ const { warnings, errors, generatedFiles } = context;
104
+ let hasWarning = false;
105
+ let hasError = false;
106
+
107
+ const handlebars = Handlebars.create();
108
+ handlebars.registerHelper('json', JSON.stringify);
109
+ handlebars.registerHelper('jspretty', (data) => {
110
+ return JSON.stringify(data).replace(/"/g, '\'');
111
+ });
112
+ handlebars.registerHelper('eq', (a, b) => a === b);
113
+ handlebars.registerHelper('markdownCompile', (data) => {
114
+ return handlebars.compile(data)(context);
115
+ });
116
+
117
+ debug('descriptors', JSON.stringify(descriptors, null, 2));
118
+
119
+ await Promise.all(descriptors.map(async desc => {
120
+ const targetDir = path.dirname(desc.targetFile);
121
+ let content;
122
+
123
+ try {
124
+ const source = await fs.readFile(desc.srcFile, { encoding: 'utf8' });
125
+ content = source;
126
+ try {
127
+ content = handlebars.compile(source)(context);
128
+ } catch (compileErr) {
129
+ hasWarning = true;
130
+ warnings.push(`Error templating ${desc.targetFile}: ${compileErr.message}`);
131
+ }
132
+ } catch (readErr) {
133
+ if (readErr.code === 'EISDIR') {
134
+ // Skipping directory errors.
135
+ // node-glob seems to incorrectly captures some directories on Windows
136
+ hasWarning = true;
137
+ warnings.push(`Directory matched as template file: ${desc.targetFile}`);
138
+ } else {
139
+ hasError = true;
140
+ errors.push(`Error reading template ${desc.srcFile}: ${readErr.message}`);
141
+ }
142
+ }
143
+ if (!content) return;
144
+
145
+ try {
146
+ await mkdirp(targetDir);
147
+ try {
148
+ await fs.writeFile(desc.targetFile, content, { encoding: 'utf8' });
149
+ const message = desc.target + (desc.overrides ? dim(` – from ${desc.from}`) : '');
150
+ generatedFiles.add(message);
151
+ } catch (writeErr) {
152
+ hasError = true;
153
+ errors.push(`Error writing ${desc.targetFile}: ${writeErr.message}`);
154
+ }
155
+ } catch (err) {
156
+ hasError = true;
157
+ errors.push(`Error creating directory ${targetDir}: ${err.message}`);
158
+ }
159
+ }));
160
+
161
+ return [hasWarning, hasError];
162
+ }
163
+
164
+ /**
165
+ * Generate the app files and templates using context
166
+ *
167
+ * @param {CreateContext} context - Create context
168
+ * @param {Spinner} spinner - Spinner
169
+ * @returns {Promise} promise
170
+ */
171
+ async function generateFiles({ context, spinner }) {
172
+ const { files } = context;
173
+
174
+ if (!files.globSets.length) {
175
+ return;
176
+ }
177
+
178
+ let descriptors = await getDescriptors(context);
179
+ descriptors = reduceDescriptors(descriptors);
180
+ const [hasWarnings, hasError] = await performGenerate(context, descriptors);
181
+
182
+ if (hasWarnings) spinner.warn();
183
+ if (hasError) spinner.fail();
184
+ }
185
+
186
+ export default action('Generate app contents', generateFiles);
187
+ // exported for unit testing
188
+ export const _getDescriptors = getDescriptors;
189
+ export const _assembleDescriptors = assembleDescriptors;