create-gasket-app 7.0.0-next.2 → 7.0.0-next.21

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 (41) hide show
  1. package/lib/commands/create.js +40 -85
  2. package/lib/config/default-plugins.js +6 -6
  3. package/lib/index.d.ts +81 -25
  4. package/lib/index.js +7 -6
  5. package/lib/main.js +5 -4
  6. package/lib/scaffold/action-wrapper.js +8 -7
  7. package/lib/scaffold/actions/create-hooks.js +8 -9
  8. package/lib/scaffold/actions/generate-files.js +17 -15
  9. package/lib/scaffold/actions/global-prompts.js +11 -26
  10. package/lib/scaffold/actions/index.js +18 -21
  11. package/lib/scaffold/actions/install-modules.js +3 -6
  12. package/lib/scaffold/actions/link-modules.js +3 -5
  13. package/lib/scaffold/actions/load-preset.js +50 -72
  14. package/lib/scaffold/actions/mkdir.js +4 -4
  15. package/lib/scaffold/actions/post-create-hooks.js +6 -8
  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 +8 -7
  19. package/lib/scaffold/actions/prompt-hooks.js +10 -57
  20. package/lib/scaffold/actions/setup-pkg.js +17 -29
  21. package/lib/scaffold/actions/write-gasket-config.js +102 -17
  22. package/lib/scaffold/actions/write-pkg.js +5 -9
  23. package/lib/scaffold/config-builder.js +82 -7
  24. package/lib/scaffold/create-context.js +15 -21
  25. package/lib/scaffold/dump-error-context.js +5 -5
  26. package/lib/scaffold/files.js +2 -2
  27. package/lib/scaffold/utils.js +4 -97
  28. package/lib/utils/create-option.js +2 -4
  29. package/lib/utils/index.js +6 -16
  30. package/lib/utils/logo.js +1 -3
  31. package/lib/utils/process-args.js +1 -3
  32. package/lib/utils/process-command.js +3 -7
  33. package/lib/utils/process-options.js +1 -3
  34. package/lib/utils/warn-if-outdated.js +11 -9
  35. package/package.json +14 -12
  36. package/lib/scaffold/actions/apply-preset-config.js +0 -29
  37. package/lib/scaffold/actions/cli-version.js +0 -84
  38. package/lib/scaffold/actions/load-pkg-for-debug.js +0 -24
  39. package/lib/scaffold/create-engine.js +0 -21
  40. package/lib/scaffold/fetcher.js +0 -194
  41. package/lib/utils/setup.js +0 -25
@@ -1,4 +1,4 @@
1
- const action = require('../action-wrapper');
1
+ import action from '../action-wrapper.js';
2
2
 
3
3
  /**
4
4
  * Installs node_modules using the selected package manager
@@ -6,13 +6,10 @@ const action = require('../action-wrapper');
6
6
  * @param {CreateContext} context - Create context
7
7
  * @returns {Promise} promise
8
8
  */
9
- async function installModules(context) {
9
+ async function installModules({ context }) {
10
10
  const { pkgManager } = context;
11
11
 
12
12
  await pkgManager.install();
13
13
  }
14
14
 
15
-
16
- module.exports = action('Install node modules', installModules);
17
-
18
- module.exports.update = action('Update node modules', installModules);
15
+ export default action('Install node modules', installModules);
@@ -1,4 +1,4 @@
1
- const action = require('../action-wrapper');
1
+ import action from '../action-wrapper.js';
2
2
 
3
3
  /**
4
4
  * Links local packages using the selected package manager
@@ -7,7 +7,7 @@ const action = require('../action-wrapper');
7
7
  * @param {Spinner} spinner - Spinner
8
8
  * @returns {Promise} promise
9
9
  */
10
- async function linkModules(context, spinner) {
10
+ async function linkModules({ context, spinner }) {
11
11
  const { pkgLinks, pkgManager } = context;
12
12
 
13
13
  if (pkgLinks && pkgLinks.length) {
@@ -16,6 +16,4 @@ async function linkModules(context, spinner) {
16
16
  }
17
17
  }
18
18
 
19
- module.exports = action('Link node modules', linkModules, { startSpinner: false });
20
-
21
- module.exports.update = action('Relink node modules', linkModules, { startSpinner: false });
19
+ export default action('Link node modules', linkModules, { startSpinner: false });
@@ -1,82 +1,60 @@
1
- const path = require('path');
2
- const action = require('../action-wrapper');
3
- const PackageFetcher = require('../fetcher');
4
- const { presetIdentifier, Loader } = require('@gasket/resolve');
5
-
6
- const loader = new Loader();
7
-
8
- /**
9
- * Fetches the preset packages and loads PresetInfos
10
- *
11
- * @param {[String]} rawPresets - Presets names
12
- * @param {String} cwd - Root path
13
- * @param {String} from from
14
- * @returns {PresetInfo[]} loaded presetInfos
15
- * @private
16
- */
17
- async function remotePresets(rawPresets, cwd, from) {
18
- if (!rawPresets) { return []; }
19
-
20
- const allRemotePresets = await Promise.all(rawPresets.map(async rawName => {
21
- const packageName = presetIdentifier(rawName).full;
22
- const fetcher = new PackageFetcher({ cwd, packageName });
23
- const pkgPath = await fetcher.clone();
24
-
25
- const presetInfo = loader.loadPreset(pkgPath, { from: from, rawName }, { shallow: true });
26
- const { name: presetName, dependencies } = presetInfo.package;
27
- if (!dependencies) {
28
- return presetInfo;
29
- }
30
-
31
- const presetDeps = Object.keys(dependencies).filter(k => presetIdentifier.isValidFullName(k));
32
- const rawPresetsDeps = presetDeps.map(presetDep => `${presetDep}@${dependencies[presetDep]}`);
33
-
34
- const presetInfoDeps = await remotePresets(rawPresetsDeps, cwd, presetName);
35
- return {
36
- ...presetInfo,
37
- presets: presetInfoDeps
38
- };
39
- }));
40
-
41
- return allRemotePresets;
42
- }
1
+ import path from 'path';
2
+ import os from 'os';
3
+ import action from '../action-wrapper.js';
4
+ import { default as gasketUtils } from '@gasket/utils';
5
+ import { mkdtemp } from 'fs/promises';
6
+ import { createRequire } from 'module';
7
+ const require = createRequire(import.meta.url);
8
+ const hasVersion = /@(\^[0-9]\.|[0-9]).+/;
43
9
 
44
10
  /**
45
- * Loads PresetInfo from local preset package
46
- *
11
+ * loadPresets - Load presets to temp directory
47
12
  * @param {CreateContext} context - Create context
48
- * @returns {PresetInfo[]} loaded presetInfos
49
- * @private
50
13
  */
51
- function localPreset(context) {
52
- const { cwd, localPresets = [] } = context;
53
- if (!localPresets) { return []; }
54
-
55
- const presetInfos = [];
56
- localPresets.forEach(localPresetPath => {
57
- const pkgPath = path.resolve(cwd, localPresetPath);
58
- const presetInfo = loader.loadPreset(pkgPath, { from: 'cli' }, { shallow: false });
59
- presetInfo.rawName = `${presetInfo.package.name}@file:${pkgPath}`;
60
- presetInfos.push(presetInfo);
14
+ async function loadPresets({ context }) {
15
+ const tmpDir = await mkdtemp(path.join(os.tmpdir(), `gasket-create-${context.appName}`));
16
+ context.tmpDir = tmpDir;
17
+
18
+ const modPath = path.join(tmpDir, 'node_modules');
19
+ const pkgManager = new gasketUtils.PackageManager({
20
+ packageManager: context.packageManager,
21
+ dest: tmpDir
22
+ });
23
+ const pkgVerb = pkgManager.isYarn ? 'add' : 'install';
24
+
25
+ const remotePresets = context.rawPresets.map(async preset => {
26
+ const parts = hasVersion.test(preset) && preset.split('@').filter(Boolean);
27
+ const name = parts ? `@${parts[0]}` : preset;
28
+ const version = parts ? `@${parts[1]}` : '@latest';
29
+
30
+ try {
31
+ // Install the preset
32
+ await pkgManager.exec(pkgVerb, [`${name}${version}`]);
33
+ // Get the package file
34
+ const pkgFile = require(path.join(modPath, name, 'package.json'));
35
+ // Import the preset via the package file attrs, name and main
36
+ // We can't specify the cwd for the import, so we need to use the full path
37
+ // expects type:module & "main": "lib/fullpath.js"
38
+ const mod = await import(`${modPath}/${name}/${pkgFile.main}`);
39
+ return mod.default || mod;
40
+ } catch (err) {
41
+ throw new Error(`Failed to install preset ${name}${version}`);
42
+ }
61
43
  });
62
44
 
63
- return presetInfos;
64
- }
45
+ const localPresets = context.localPresets.map(async localPresetPath => {
65
46
 
66
- /**
67
- * Downloads the target preset package and adds the package.json
68
- * contents and plugins dependencies to context.
69
- *
70
- * @param {CreateContext} context - Create context
71
- * @returns {Promise} promise
72
- */
73
- async function loadPreset(context) {
74
- const { rawPresets = [], cwd } = context;
75
- let presetInfos = await remotePresets(rawPresets, cwd, 'cli');
76
- presetInfos = presetInfos.concat(localPreset(context));
47
+ try {
48
+ await pkgManager.exec(pkgVerb, [localPresetPath]);
49
+ const pkgFile = require(path.join(localPresetPath, 'package.json'));
50
+ const mod = await import(`${modPath}/${pkgFile.name}/${pkgFile.main}`);
51
+ return mod.default || mod;
52
+ } catch (err) {
53
+ throw new Error(`Failed to install local preset ${localPresetPath}`);
54
+ }
55
+ });
77
56
 
78
- const presets = presetInfos.map(p => presetIdentifier(p.rawName).shortName);
79
- Object.assign(context, { presets, presetInfos });
57
+ context.presets = await Promise.all([...remotePresets, ...localPresets]);
80
58
  }
81
59
 
82
- module.exports = action('Load presets', loadPreset);
60
+ export default action('Load presets', loadPresets);
@@ -1,5 +1,5 @@
1
- const { mkdir } = require('fs').promises;
2
- const action = require('../action-wrapper');
1
+ import { mkdir } from 'fs/promises';
2
+ import action from '../action-wrapper.js';
3
3
 
4
4
  /**
5
5
  * Validates this instance can execute without common blockers:
@@ -10,7 +10,7 @@ const action = require('../action-wrapper');
10
10
  * @param {Spinner} spinner - Spinner
11
11
  * @returns {Promise} promise
12
12
  */
13
- async function mkDir(context, spinner) {
13
+ async function mkDir({ context, spinner }) {
14
14
  const { dest, relDest, extant, destOverride } = context;
15
15
 
16
16
  // At this point, either the directory does not exist, or the user has
@@ -27,4 +27,4 @@ async function mkDir(context, spinner) {
27
27
  }
28
28
  }
29
29
 
30
- module.exports = action('Set up directory', mkDir);
30
+ export default action('Set up directory', mkDir);
@@ -1,15 +1,15 @@
1
- const action = require('../action-wrapper');
2
- const createEngine = require('../create-engine');
3
- const { runShellCommand } = require('@gasket/utils');
1
+ import action from '../action-wrapper.js';
2
+ import { runShellCommand } from '@gasket/utils';
4
3
 
5
4
  /**
6
5
  * Executes the `postCreate` hook for all registered plugins.
7
6
  *
7
+ * @param {GasketEngine} gasket - Gasket API
8
8
  * @param {CreateContext} context - Create context
9
9
  * @returns {Promise} promise
10
10
  */
11
- async function postCreateHooks(context) {
12
- const { dest, presets = [], plugins = [] } = context;
11
+ async function postCreateHooks({ gasket, context }) {
12
+ const { dest } = context;
13
13
 
14
14
  /**
15
15
  * Run an npm script in the context of the created application
@@ -25,9 +25,7 @@ async function postCreateHooks(context) {
25
25
  * in future is easy.
26
26
  */
27
27
  const utils = { runScript };
28
-
29
- const gasket = await createEngine({ dest, presets, plugins });
30
28
  await gasket.exec('postCreate', context, utils);
31
29
  }
32
30
 
33
- module.exports = action('Execute postCreate hooks', postCreateHooks);
31
+ export default action('Execute postCreate hooks', postCreateHooks);
@@ -0,0 +1,13 @@
1
+ import action from '../action-wrapper.js';
2
+
3
+ /**
4
+ * presetConfigHooks - exec `presetConfig` hook
5
+ * @param {Gasket} gasket - Preset gasket instance
6
+ * @param {CreateContext} context - Create context
7
+ */
8
+ async function presetConfigHooks({ gasket, context }) {
9
+ const config = await gasket.execWaterfall('presetConfig', context);
10
+ Object.assign(context.presetConfig, config);
11
+ }
12
+
13
+ export default action('Preset Config', presetConfigHooks, { startSpinner: false });
@@ -0,0 +1,17 @@
1
+ import inquirer from 'inquirer';
2
+ import action from '../action-wrapper.js';
3
+
4
+ /**
5
+ * presetPromptHooks - exec `presetPrompt` hook
6
+ * @param {Gasket} gasket - Preset gasket instance
7
+ * @param {CreateContext} context - Create context
8
+ */
9
+ async function presetPromptHooks({ gasket, context }) {
10
+ const prompt = context.prompts ? inquirer.createPromptModule() : () => ({});
11
+
12
+ const nextContext = await gasket.execWaterfall('presetPrompt', context, { prompt });
13
+
14
+ Object.assign(context, nextContext);
15
+ }
16
+
17
+ export default action('Preset prompts', presetPromptHooks, { startSpinner: false });
@@ -1,6 +1,7 @@
1
- const { bold } = require('chalk');
2
- const action = require('../action-wrapper');
3
- const asciiLogo = require('../../utils/logo');
1
+ import pkg from 'chalk';
2
+ const { bold } = pkg;
3
+ import action from '../action-wrapper.js';
4
+ import { logo } from '../../utils/index.js';
4
5
 
5
6
  /**
6
7
  * Logs a new line in the console
@@ -62,14 +63,14 @@ function buildReport(context) {
62
63
 
63
64
  * @param {CreateContext} context - Create context
64
65
  */
65
- function printReport(context) {
66
+ async function printReport({ context }) {
66
67
  const report = buildReport(context);
67
68
  const { warnings, errors } = context;
68
69
 
69
70
  console.log(`✨Success!
70
-
71
+
71
72
  Finished with ${warnings.length} warnings and ${errors.length} errors using
72
- ` + asciiLogo);
73
+ ` + logo);
73
74
 
74
75
  Object.entries(report).forEach(([k, v]) => {
75
76
  if (!v || !v.length) return;
@@ -85,4 +86,4 @@ Finished with ${warnings.length} warnings and ${errors.length} errors using
85
86
  newline();
86
87
  }
87
88
 
88
- module.exports = action('Print report', printReport, { startSpinner: false });
89
+ export default action('Print report', printReport, { startSpinner: false });
@@ -1,47 +1,5 @@
1
- const inquirer = require('inquirer');
2
- const action = require('../action-wrapper');
3
- const { addPluginsToContext, addPluginsToPkg, getPluginsWithVersions } = require('../utils');
4
- const createEngine = require('../create-engine');
5
-
6
- /**
7
- * Create the `addPlugins` function with context
8
- *
9
- * @param {CreateContext} context - Create context
10
- * @returns {addPlugins} addPlugins
11
- */
12
- const createAddPlugins = context => {
13
- /**
14
- * Allows plugins to add plugins based on user input.
15
- *
16
- * @typedef {Function} addPlugins
17
- *
18
- * @param {...PluginDesc} pluginsToAdd - Additional plugins to add
19
- * @returns {Promise} promise
20
- */
21
- return async function addPlugins(...pluginsToAdd) {
22
- const { pkg, pkgLinks = [], pkgManager } = context;
23
-
24
- addPluginsToContext(pluginsToAdd, context);
25
- const pluginIds = await getPluginsWithVersions(pluginsToAdd, pkgManager);
26
- addPluginsToPkg(pluginIds, pkg);
27
-
28
- //
29
- // Install new plugins if not already linked
30
- //
31
- const toInstall = pluginIds.filter(p => !pkgLinks.includes(p.fullName));
32
- if (toInstall.length) {
33
- await pkgManager.install(toInstall.map(p => p.full));
34
- //
35
- // relink because npm blows them away on installs...
36
- //
37
- if (pkgLinks.length) {
38
- await pkgManager.link(pkgLinks);
39
- }
40
- }
41
-
42
- await execPluginPrompts(context, pluginIds.map(p => p.shortName));
43
- };
44
- };
1
+ import inquirer from 'inquirer';
2
+ import action from '../action-wrapper.js';
45
3
 
46
4
  /**
47
5
  * Initializes engine with provide preset and plugins
@@ -53,18 +11,12 @@ const createAddPlugins = context => {
53
11
  * @returns {Promise} promise
54
12
  * @private
55
13
  */
56
- async function execPluginPrompts(context, plugins = [], presets = []) {
57
- const { dest } = context;
58
-
59
- const gasket = await createEngine({ dest, presets, plugins });
60
-
14
+ async function execPluginPrompts(gasket, context) {
61
15
  //
62
16
  // @see: https://github.com/SBoudrias/Inquirer.js/#inquirercreatepromptmodule---prompt-function
63
17
  //
64
18
  const prompt = context.prompts ? inquirer.createPromptModule() : () => ({});
65
- const addPlugins = createAddPlugins(context);
66
-
67
- const nextContext = await gasket.execWaterfall('prompt', context, { prompt, addPlugins });
19
+ const nextContext = await gasket.execWaterfall('prompt', context, { prompt });
68
20
  //
69
21
  // Ensure the original context is transformed in case a prompt returns a new object
70
22
  //
@@ -73,18 +25,19 @@ async function execPluginPrompts(context, plugins = [], presets = []) {
73
25
 
74
26
  /**
75
27
  * Executes the `prompt` hook for all registered plugins.
76
- * Adds `prompt` and `addPlugins` util functions for prompting features.
28
+ * Adds `prompt` util function for prompting features.
77
29
  *
30
+ * @param {GasketEngine} gasket - Gasket API
78
31
  * @param {CreateContext} context - Create context
79
32
  * @returns {Promise} promise
80
33
  */
81
- async function promptHooks(context) {
34
+ async function promptHooks({ gasket, context }) {
82
35
  //
83
36
  // Because `execPluginPrompts` is recursively, we need to start it
84
37
  // with the processPlugins and presets from our initial context
85
38
  //
86
- const { presets, plugins } = context;
87
- await execPluginPrompts(context, plugins, presets);
39
+ const { plugins } = context;
40
+ await execPluginPrompts(gasket, context, plugins);
88
41
  }
89
42
 
90
- module.exports = action('Plugin prompts', promptHooks, { startSpinner: false });
43
+ export default action('Plugin prompts', promptHooks, { startSpinner: false });
@@ -1,9 +1,9 @@
1
- const action = require('../action-wrapper');
2
- const ConfigBuilder = require('../config-builder');
3
- const { addPluginsToPkg, getPluginsWithVersions } = require('../utils');
4
- const { PackageManager } = require('@gasket/utils');
5
- const { presetIdentifier } = require('@gasket/resolve');
6
- const { devDependencies } = require('../../../package');
1
+ import action from '../action-wrapper.js';
2
+ import { ConfigBuilder } from '../config-builder.js';
3
+ import { default as gasketUtils } from '@gasket/utils';
4
+ import { createRequire } from 'module';
5
+ const require = createRequire(import.meta.url);
6
+ const { dependencies } = require('../../../package.json');
7
7
 
8
8
  /**
9
9
  * Initializes the ConfigBuilder builder and adds to context.
@@ -11,37 +11,25 @@ const { devDependencies } = require('../../../package');
11
11
  * @param {CreateContext} context - Create context
12
12
  * @returns {Promise} promise
13
13
  */
14
- async function setupPkg(context) {
15
- const { appName, appDescription, presetInfos = [], rawPlugins = [], cliVersionRequired, warnings } = context;
14
+ async function setupPkg({ context }) {
15
+ const { appName, appDescription, warnings } = context;
16
16
 
17
17
  const pkg = ConfigBuilder.createPackageJson({
18
18
  name: appName,
19
19
  version: '0.0.0',
20
- description: appDescription
20
+ description: appDescription,
21
+ type: context.typescript ? 'commonjs' : 'module'
21
22
  }, { warnings });
22
23
 
23
- // The preset package itself must be included in the dependencies
24
- // of the `pkg` to be bootstrapped.
25
- pkg.add('dependencies', presetInfos.reduce((acc, presetInfo) => {
26
- // Use rawName in case version or file path was set in cli args
27
- // Otherwise fallback to resolved version
28
- const { fullName, version } = presetIdentifier(presetInfo.rawName).withVersion(`^${presetInfo.version}`);
29
-
30
- acc[fullName] = version;
31
- return acc;
32
- }, {
33
- '@gasket/cli': cliVersionRequired
34
- }));
35
-
36
- pkg.add('depencencies', {
37
- dotenv: devDependencies.dotenv
24
+ // Add critical dependencies
25
+ pkg.add('dependencies', {
26
+ '@gasket/core': dependencies['@gasket/core'],
27
+ '@gasket/engine': dependencies['@gasket/engine'],
28
+ '@gasket/utils': dependencies['@gasket/utils']
38
29
  });
39
30
 
40
- const pkgManager = new PackageManager(context);
41
- const pluginIds = await getPluginsWithVersions(rawPlugins, pkgManager);
42
- addPluginsToPkg(pluginIds, pkg);
43
-
31
+ const pkgManager = new gasketUtils.PackageManager(context);
44
32
  Object.assign(context, { pkg, pkgManager });
45
33
  }
46
34
 
47
- module.exports = action('Set up package.json', setupPkg, { startSpinner: false });
35
+ export default action('Set up package.json', setupPkg, { startSpinner: false });
@@ -1,7 +1,86 @@
1
- const JSON5 = require('json5');
2
- const path = require('path');
3
- const { writeFile } = require('fs').promises;
4
- const action = require('../action-wrapper');
1
+ import { default as JSON5 } from 'json5';
2
+ import path from 'path';
3
+ import { writeFile } from 'fs/promises';
4
+ import action from '../action-wrapper.js';
5
+
6
+ /**
7
+ * writePluginImports - Write string imports as value(s)
8
+ * @param {string[]} plugins - Array of plugin import names
9
+ * @returns {string} - Array of plugin imports as string
10
+ */
11
+ function writePluginImports(plugins) {
12
+ return plugins.reduce((acc, cur, index) => {
13
+ acc += `\t\t${cur}${index < plugins.length - 1 ? ',\n' : ''}`;
14
+ return acc;
15
+ }, '');
16
+ }
17
+
18
+ /**
19
+ * writeImports - Write imports to file using key value pairs
20
+ * @param {{[pluginImport: string]: string}[]} imports - Array of key value pairs for imports
21
+ * @returns {string} - import declarations as string
22
+ */
23
+ function writeImports(imports) {
24
+ if (!imports) return '';
25
+ return `${Object.entries(imports).map(([importDef, importPath]) => `import ${importDef} from '${importPath}';`).join('\n')}\n`;
26
+ }
27
+
28
+ /**
29
+ * createInjectionAssignments - replace object path with a temp string value
30
+ * @param {GasketConfigDefinition} config - gasket config object
31
+ * @param {{ [configKey]: string }} assignments - key value pairs of config keys to replace
32
+ * @returns {string} - string if assignments is falsy
33
+ */
34
+ function createInjectionAssignments(config, assignments) {
35
+ if (!assignments) return '';
36
+ Object.keys(assignments).forEach((key) => {
37
+ const keys = key.split('.');
38
+
39
+ keys.reduce((acc, cur, index) => {
40
+ if (index === keys.length - 1) {
41
+ acc[cur] = `${key}_INJECTION_ASSIGNMENT_REPLACE`;
42
+ return;
43
+ }
44
+ return acc[cur];
45
+ }, config);
46
+ });
47
+ }
48
+
49
+ /**
50
+ * replaceInjectionAssignments - replace temp string value with actual value
51
+ * @param {string} content
52
+ * @param {{ [configKey]: string }} assignments - key value pairs of config keys to replace
53
+ * @returns {string} - content with replaced values
54
+ */
55
+ function replaceInjectionAssignments(content, assignments) {
56
+ if (!assignments) return content;
57
+ Object.keys(assignments).forEach((key) => {
58
+ content = content.replace(`'${key}_INJECTION_ASSIGNMENT_REPLACE'`, assignments[key]);
59
+ });
60
+
61
+ return content;
62
+ }
63
+
64
+ /**
65
+ * writeExpressions - Write expressions to file
66
+ * @param {string} expressions - programmatic expressions as string
67
+ * @returns {string} - expressions as string
68
+ */
69
+ function writeExpressions(expressions) {
70
+ if (!expressions) return '';
71
+ return `${expressions.map((expression) => `${expression}`).join('\n')}\n`;
72
+ }
73
+
74
+ /**
75
+ * cleanupFields - Remove fields from config object
76
+ * @param {GasketConfigDefinition} config - gasket config object
77
+ */
78
+ function cleanupFields(config) {
79
+ delete config.fields.imports;
80
+ delete config.fields.pluginImports;
81
+ delete config.fields.expressions;
82
+ delete config.fields.injectionAssignments;
83
+ }
5
84
 
6
85
  /**
7
86
  * Writes the contents of `pkg` to the app's package.json.
@@ -9,24 +88,30 @@ const action = require('../action-wrapper');
9
88
  * @param {CreateContext} context - Create context
10
89
  * @returns {Promise} promise
11
90
  */
12
- async function writeGasketConfig(context) {
13
- const { dest, gasketConfig, presets, plugins, generatedFiles } = context;
14
- const fileName = 'gasket.config.js';
91
+ async function writeGasketConfig({ context }) {
92
+ const { dest, gasketConfig, generatedFiles } = context;
93
+ const fileName = context.typescript ? 'gasket.ts' : 'gasket.js';
15
94
  const filePath = path.join(dest, fileName);
95
+ const plugins = gasketConfig.fields.plugins;
96
+ const assignments = gasketConfig.fields.injectionAssignments || null;
97
+ const expressions = gasketConfig.fields.expressions || null;
98
+ gasketConfig.fields.plugins = 'PLUGIN_REPLACE';
16
99
 
17
- gasketConfig.add('plugins', {
18
- presets,
19
- ...(plugins && plugins.length ? { add: plugins } : {})
20
- });
100
+ let contents = '';
101
+ contents += `import { makeGasket } from '@gasket/core';\n`;
102
+ contents += writeImports(gasketConfig.fields.pluginImports);
103
+ contents += writeImports(gasketConfig.fields.imports);
104
+ contents += writeExpressions(expressions);
105
+ createInjectionAssignments(gasketConfig.fields, assignments);
106
+ cleanupFields(gasketConfig);
21
107
 
22
- //
23
- // Use JSON5 to stringify the config and add export for CommonJS
24
- //
25
- const contents = 'module.exports = ' +
26
- JSON5.stringify(gasketConfig, null, 2) + ';\n';
108
+ const pluginImports = `[\n${writePluginImports(plugins)}\n\t]`;
109
+ contents += `\nexport default makeGasket(${JSON5.stringify(gasketConfig.fields, null, 2)});\n`;
110
+ contents = contents.replace('\'PLUGIN_REPLACE\'', pluginImports);
111
+ contents = replaceInjectionAssignments(contents, assignments);
27
112
 
28
113
  await writeFile(filePath, contents, 'utf8');
29
114
  generatedFiles.add(fileName);
30
115
  }
31
116
 
32
- module.exports = action('Write gasket config', writeGasketConfig);
117
+ export default action('Write gasket config', writeGasketConfig);
@@ -1,23 +1,19 @@
1
- const path = require('path');
2
- const { writeFile } = require('fs').promises;
3
- const action = require('../action-wrapper');
1
+ import path from 'path';
2
+ import { writeFile } from 'fs/promises';
3
+ import action from '../action-wrapper.js';
4
4
 
5
5
  /**
6
6
  * Writes the contents of `pkg` to the app's package.json.
7
7
  *
8
8
  * @param {CreateContext} context - Create context
9
- * @param {Boolean} update - Is an update step
10
9
  * @returns {Promise} promise
11
10
  */
12
- async function writePkg(context) {
11
+ async function writePkg({ context }) {
13
12
  const { dest, pkg, generatedFiles } = context;
14
13
  const fileName = 'package.json';
15
14
  const filePath = path.join(dest, fileName);
16
-
17
15
  await writeFile(filePath, JSON.stringify(pkg, null, 2), 'utf8');
18
16
  generatedFiles.add(fileName);
19
17
  }
20
18
 
21
- module.exports = action('Write package.json', writePkg);
22
-
23
- module.exports.update = action('Update package.json', writePkg);
19
+ export default action('Write package.json', writePkg);