@form8ion/javascript 7.2.0 → 7.2.3

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/lib/index.mjs CHANGED
@@ -1,12 +1,12 @@
1
1
  import { questionNames as questionNames$2, questions } from '@travi/language-scaffolder-prompts';
2
2
  import deepmerge from 'deepmerge';
3
- import { validateOptions, scaffoldChoice, dialects, projectTypes, writePackageJson, installDependencies, PROD_DEPENDENCY_TYPE, DEV_DEPENDENCY_TYPE, packageManagers, mergeIntoExistingPackageJson } from '@form8ion/javascript-core';
3
+ import { validateOptions, scaffoldChoice, projectTypes, dialects as dialects$1, writePackageJson, packageManagers, DEV_DEPENDENCY_TYPE, PROD_DEPENDENCY_TYPE, mergeIntoExistingPackageJson } from '@form8ion/javascript-core';
4
4
  import joi from 'joi';
5
5
  import { prompt as prompt$1, Separator } from '@form8ion/overridable-prompts';
6
6
  import { scaffold, lift as lift$3 } from '@form8ion/codecov';
7
7
  import { promises } from 'fs';
8
- import { fileExists, applyEnhancers, fileTypes } from '@form8ion/core';
9
- import { info, error, warn } from '@travi/cli-messages';
8
+ import { fileExists, fileTypes, writeConfigFile, applyEnhancers } from '@form8ion/core';
9
+ import { info, warn, error } from '@travi/cli-messages';
10
10
  import * as commitConventionPlugin from '@form8ion/commit-convention';
11
11
  import { scaffold as scaffold$5 } from '@form8ion/commit-convention';
12
12
  import hoek from '@hapi/hoek';
@@ -24,7 +24,7 @@ import { resolve } from 'path';
24
24
  import filedirname from 'filedirname';
25
25
  import * as huskyPlugin from '@form8ion/husky';
26
26
  import { scaffold as scaffold$2 } from '@form8ion/husky';
27
- import { write } from '@form8ion/config-file';
27
+ import { write as write$1 } from '@form8ion/config-file';
28
28
  import { scaffold as scaffold$4 } from '@form8ion/prettier';
29
29
  import { lift as lift$4, scaffold as scaffold$3 } from '@form8ion/eslint';
30
30
 
@@ -177,6 +177,90 @@ var enginesEnhancer = /*#__PURE__*/Object.freeze({
177
177
  lift: lift$1
178
178
  });
179
179
 
180
+ function write ({projectRoot, config}) {
181
+ return write$1({path: projectRoot, name: 'babel', format: fileTypes.JSON, config});
182
+ }
183
+
184
+ async function addIgnore ({projectRoot, ignore}) {
185
+ if (ignore) {
186
+ const existingConfig = JSON.parse(await promises.readFile(`${projectRoot}/.babelrc.json`, 'utf-8'));
187
+
188
+ await write({projectRoot, config: {...existingConfig, ignore: [`./${ignore}/`]}});
189
+ }
190
+ }
191
+
192
+ async function scaffoldBabel ({projectRoot, preset}) {
193
+ if (!preset) {
194
+ throw new Error('No babel preset provided. Cannot configure babel transpilation');
195
+ }
196
+
197
+ await write({projectRoot, config: {presets: [preset.name]}});
198
+
199
+ return {
200
+ devDependencies: ['@babel/register', preset.packageName],
201
+ eslint: {}
202
+ };
203
+ }
204
+
205
+ async function lifter ({buildDirectory, projectRoot}) {
206
+ await addIgnore({ignore: buildDirectory, projectRoot});
207
+
208
+ return {};
209
+ }
210
+
211
+ function predicate ({projectRoot}) {
212
+ return fileExists(`${projectRoot}/.babelrc.json`);
213
+ }
214
+
215
+ async function scaffoldTypescript ({config, projectType, projectRoot, testFilenamePattern}) {
216
+ const eslintConfigs = ['typescript'];
217
+ const shareableTsConfigPackage = `${config.scope}/tsconfig`;
218
+
219
+ await writeConfigFile({
220
+ path: projectRoot,
221
+ name: 'tsconfig',
222
+ format: fileTypes.JSON,
223
+ config: {
224
+ $schema: 'https://json.schemastore.org/tsconfig',
225
+ extends: shareableTsConfigPackage,
226
+ compilerOptions: {
227
+ rootDir: 'src',
228
+ ...projectTypes.PACKAGE === projectType && {
229
+ outDir: 'lib',
230
+ declaration: true
231
+ }
232
+ },
233
+ include: ['src/**/*.ts'],
234
+ ...testFilenamePattern && {exclude: [testFilenamePattern]}
235
+ }
236
+ });
237
+
238
+ return {
239
+ eslint: {configs: eslintConfigs},
240
+ eslintConfigs,
241
+ devDependencies: ['typescript', shareableTsConfigPackage],
242
+ vcsIgnore: {files: ['tsconfig.tsbuildinfo']}
243
+ };
244
+ }
245
+
246
+ function scaffoldDialect ({dialect, projectType, projectRoot, configs, testFilenamePattern}) {
247
+ switch (dialect) {
248
+ case dialects$1.BABEL:
249
+ return scaffoldBabel({preset: configs.babelPreset, projectRoot});
250
+ case dialects$1.TYPESCRIPT:
251
+ return scaffoldTypescript({config: configs.typescript, projectType, projectRoot, testFilenamePattern});
252
+ default:
253
+ return {};
254
+ }
255
+ }
256
+
257
+ var dialects = /*#__PURE__*/Object.freeze({
258
+ __proto__: null,
259
+ scaffold: scaffoldDialect,
260
+ test: predicate,
261
+ lift: lifter
262
+ });
263
+
180
264
  function scaffoldScripts () {
181
265
  return {};
182
266
  }
@@ -234,7 +318,7 @@ function buildPackageDetails ({
234
318
  name: packageName,
235
319
  description,
236
320
  license,
237
- type: dialects.ESM === dialect ? 'module' : 'commonjs',
321
+ type: dialects$1.ESM === dialect ? 'module' : 'commonjs',
238
322
  ...defineVcsHostDetails(vcs, projectType, packageName, pathWithinParent),
239
323
  author: `${author.name}${author.email ? ` <${author.email}>` : ''}${author.url ? ` (${author.url})` : ''}`,
240
324
  scripts: scaffoldScripts()
@@ -270,6 +354,52 @@ async function scaffoldPackage ({
270
354
  return {homepage: packageData.homepage};
271
355
  }
272
356
 
357
+ const details = {
358
+ [packageManagers.NPM]: {
359
+ installationCommand: 'install',
360
+ installationFlags: {
361
+ [DEV_DEPENDENCY_TYPE]: `save-${DEV_DEPENDENCY_TYPE}`,
362
+ [PROD_DEPENDENCY_TYPE]: `save-${PROD_DEPENDENCY_TYPE}`,
363
+ exact: 'save-exact'
364
+ }
365
+ },
366
+ [packageManagers.YARN]: {
367
+ installationCommand: 'add',
368
+ installationFlags: {
369
+ [DEV_DEPENDENCY_TYPE]: DEV_DEPENDENCY_TYPE,
370
+ [PROD_DEPENDENCY_TYPE]: PROD_DEPENDENCY_TYPE,
371
+ exact: 'exact'
372
+ }
373
+ }
374
+ };
375
+
376
+ function getInstallationCommandFor(manager) {
377
+ return details[manager].installationCommand;
378
+ }
379
+
380
+ function getDependencyTypeFlag(manager, type) {
381
+ return details[manager].installationFlags[type];
382
+ }
383
+
384
+ function getExactFlag(manager) {
385
+ return details[manager].installationFlags.exact;
386
+ }
387
+
388
+ async function install$1 (dependencies, dependenciesType, projectRoot, packageManager = packageManagers.NPM) {
389
+ if (dependencies.length) {
390
+ info(`Installing ${dependenciesType} dependencies`, {level: 'secondary'});
391
+
392
+ await execa(
393
+ `. ~/.nvm/nvm.sh && nvm use && ${packageManager} ${
394
+ getInstallationCommandFor(packageManager)
395
+ } ${[...new Set(dependencies)].join(' ')} --${getDependencyTypeFlag(packageManager, dependenciesType)}${
396
+ DEV_DEPENDENCY_TYPE === dependenciesType ? ` --${getExactFlag(packageManager)}` : ''
397
+ }`,
398
+ {shell: true, cwd: projectRoot}
399
+ );
400
+ } else warn(`No ${dependenciesType} dependencies to install`);
401
+ }
402
+
273
403
  async function liftPackage ({
274
404
  projectRoot,
275
405
  scripts,
@@ -300,8 +430,8 @@ async function liftPackage ({
300
430
  info('Installing dependencies');
301
431
 
302
432
  try {
303
- await installDependencies(dependencies || [], PROD_DEPENDENCY_TYPE, projectRoot, packageManager);
304
- await installDependencies([...devDependencies || []], DEV_DEPENDENCY_TYPE, projectRoot, packageManager);
433
+ await install$1(dependencies || [], PROD_DEPENDENCY_TYPE, projectRoot, packageManager);
434
+ await install$1([...devDependencies || []], DEV_DEPENDENCY_TYPE, projectRoot, packageManager);
305
435
  } catch (e) {
306
436
  error('Failed to install dependencies');
307
437
  }
@@ -324,15 +454,24 @@ async function resolvePackageManager ({projectRoot, packageManager}) {
324
454
  async function lift ({projectRoot, vcs, results}) {
325
455
  info('Lifting JavaScript-specific details');
326
456
 
327
- const {scripts, tags, eslintConfigs, eslint, dependencies, devDependencies, packageManager: manager} = results;
457
+ const {
458
+ scripts,
459
+ tags,
460
+ eslintConfigs,
461
+ eslint,
462
+ dependencies,
463
+ devDependencies,
464
+ packageManager: manager,
465
+ buildDirectory
466
+ } = results;
328
467
 
329
468
  const packageManager = await resolvePackageManager({projectRoot, packageManager: manager});
330
469
 
331
470
  const eslintResults = await lift$4({projectRoot, configs: [...eslintConfigs || [], ...eslint?.configs || []]});
332
471
  const enhancerResults = await applyEnhancers({
333
472
  results,
334
- enhancers: [huskyPlugin, enginesEnhancer, coveragePlugin, commitConventionPlugin],
335
- options: {packageManager, projectRoot, vcs}
473
+ enhancers: [huskyPlugin, enginesEnhancer, coveragePlugin, commitConventionPlugin, dialects],
474
+ options: {packageManager, projectRoot, vcs, buildDirectory}
336
475
  });
337
476
 
338
477
  await liftPackage(
@@ -434,10 +573,10 @@ function validate(options) {
434
573
 
435
574
  function buildDialectChoices ({babelPreset, typescript}) {
436
575
  return [
437
- {name: 'Common JS (no transpilation)', value: dialects.COMMON_JS, short: 'cjs'},
438
- ...babelPreset ? [{name: 'Modern JavaScript (transpiled)', value: dialects.BABEL, short: 'modern'}] : [],
439
- {name: 'ESM-only (no transpilation)', value: dialects.ESM, short: 'esm'},
440
- ...typescript ? [{name: 'TypeScript', value: dialects.TYPESCRIPT, short: 'ts'}] : []
576
+ {name: 'Common JS (no transpilation)', value: dialects$1.COMMON_JS, short: 'cjs'},
577
+ ...babelPreset ? [{name: 'Modern JavaScript (transpiled)', value: dialects$1.BABEL, short: 'modern'}] : [],
578
+ {name: 'ESM-only (no transpilation)', value: dialects$1.ESM, short: 'esm'},
579
+ ...typescript ? [{name: 'TypeScript', value: dialects$1.TYPESCRIPT, short: 'ts'}] : []
441
580
  ];
442
581
  }
443
582
 
@@ -620,64 +759,6 @@ async function prompt(
620
759
  };
621
760
  }
622
761
 
623
- async function scaffoldBabel ({projectRoot, preset, buildDirectory}) {
624
- if (!preset) {
625
- throw new Error('No babel preset provided. Cannot configure babel transpilation');
626
- }
627
-
628
- await write({
629
- path: projectRoot,
630
- name: 'babel',
631
- format: fileTypes.JSON,
632
- config: {presets: [preset.name], ignore: [`./${buildDirectory}/`]}
633
- });
634
-
635
- return {
636
- devDependencies: ['@babel/register', preset.packageName],
637
- eslint: {}
638
- };
639
- }
640
-
641
- async function scaffoldTypescript ({config, projectType, projectRoot, testFilenamePattern}) {
642
- const eslintConfigs = ['typescript'];
643
- const shareableTsConfigPackage = `${config.scope}/tsconfig`;
644
-
645
- await promises.writeFile(
646
- `${projectRoot}/tsconfig.json`,
647
- JSON.stringify({
648
- $schema: 'https://json.schemastore.org/tsconfig',
649
- extends: shareableTsConfigPackage,
650
- compilerOptions: {
651
- rootDir: 'src',
652
- ...projectTypes.PACKAGE === projectType && {
653
- outDir: 'lib',
654
- declaration: true
655
- }
656
- },
657
- include: ['src/**/*.ts'],
658
- ...testFilenamePattern && {exclude: [testFilenamePattern]}
659
- })
660
- );
661
-
662
- return {
663
- eslint: {configs: eslintConfigs},
664
- eslintConfigs,
665
- devDependencies: ['typescript', shareableTsConfigPackage],
666
- vcsIgnore: {files: ['tsconfig.tsbuildinfo']}
667
- };
668
- }
669
-
670
- function scaffoldDialect ({dialect, projectType, projectRoot, configs, buildDirectory, testFilenamePattern}) {
671
- switch (dialect) {
672
- case dialects.BABEL:
673
- return scaffoldBabel({preset: configs.babelPreset, projectRoot, buildDirectory});
674
- case dialects.TYPESCRIPT:
675
- return scaffoldTypescript({config: configs.typescript, projectType, projectRoot, testFilenamePattern});
676
- default:
677
- return {};
678
- }
679
- }
680
-
681
762
  function projectWillNotBeConsumed(projectType) {
682
763
  return projectTypes.APPLICATION === projectType || projectTypes.CLI === projectType;
683
764
  }
@@ -885,7 +966,7 @@ async function buildDetails ({
885
966
  dialect,
886
967
  decisions
887
968
  }) {
888
- if (dialects.COMMON_JS === dialect) return buildDetailsForCommonJsProject({projectRoot, projectName});
969
+ if (dialects$1.COMMON_JS === dialect) return buildDetailsForCommonJsProject({projectRoot, projectName});
889
970
 
890
971
  const chosenBundler = await chooseBundler({bundlers: packageBundlers, decisions});
891
972
 
@@ -942,14 +1023,14 @@ async function scaffoldPackageType ({
942
1023
  mergeIntoExistingPackageJson({
943
1024
  projectRoot,
944
1025
  config: {
945
- files: ['example.js', ...dialects.COMMON_JS === dialect ? ['index.js'] : ['lib/']],
1026
+ files: ['example.js', ...dialects$1.COMMON_JS === dialect ? ['index.js'] : ['lib/']],
946
1027
  publishConfig: {
947
1028
  access: 'Public' === visibility ? 'public' : 'restricted',
948
1029
  ...publishRegistry && {registry: publishRegistry}
949
1030
  },
950
1031
  sideEffects: false,
951
1032
  ...'Public' === visibility && {runkitExampleFilename: './example.js'},
952
- ...dialects.BABEL === dialect && {
1033
+ ...dialects$1.BABEL === dialect && {
953
1034
  main: './lib/index.js',
954
1035
  module: './lib/index.mjs',
955
1036
  exports: {
@@ -957,11 +1038,11 @@ async function scaffoldPackageType ({
957
1038
  import: './lib/index.mjs'
958
1039
  }
959
1040
  },
960
- ...dialects.ESM === dialect && {
1041
+ ...dialects$1.ESM === dialect && {
961
1042
  main: './lib/index.mjs',
962
1043
  exports: './lib/index.mjs'
963
1044
  },
964
- ...dialects.TYPESCRIPT === dialect && {
1045
+ ...dialects$1.TYPESCRIPT === dialect && {
965
1046
  main: './lib/index.js',
966
1047
  module: './lib/index.mjs',
967
1048
  types: './lib/index.d.ts',
@@ -1272,7 +1353,7 @@ async function scaffoldEslint ({config, projectRoot, buildDirectory, additionalC
1272
1353
  }
1273
1354
 
1274
1355
  async function scaffoldRemark ({config, projectRoot, projectType, vcs, dialect}) {
1275
- await write({
1356
+ await write$1({
1276
1357
  format: fileTypes.JSON,
1277
1358
  path: projectRoot,
1278
1359
  name: 'remark',
@@ -1304,7 +1385,7 @@ async function scaffoldRemark ({config, projectRoot, projectType, vcs, dialect})
1304
1385
  {
1305
1386
  ...projectTypes.PACKAGE === projectType && {
1306
1387
  devDependencies: ['remark-usage'],
1307
- ...dialects.COMMON_JS !== dialect && {scripts: {'pregenerate:md': 'run-s build'}}
1388
+ ...dialects$1.COMMON_JS !== dialect && {scripts: {'pregenerate:md': 'run-s build'}}
1308
1389
  }
1309
1390
  }
1310
1391
  );
@@ -1430,7 +1511,6 @@ async function scaffolder (options) {
1430
1511
  configs,
1431
1512
  projectRoot,
1432
1513
  projectType,
1433
- buildDirectory: projectTypeResults.buildDirectory,
1434
1514
  testFilenamePattern: verificationResults.testFilenamePattern
1435
1515
  }),
1436
1516
  scaffoldCodeStyle({
@@ -1470,8 +1550,8 @@ async function scaffolder (options) {
1470
1550
  scaffoldChoice(ciServices, ci, {projectRoot, vcs, visibility, projectType, projectName, nodeVersion, tests}),
1471
1551
  scaffold$5({projectRoot, projectType, configs, pathWithinParent})
1472
1552
  ])),
1473
- projectTypePluginResults,
1474
1553
  projectTypeResults,
1554
+ projectTypePluginResults,
1475
1555
  verificationResults,
1476
1556
  codeStyleResults,
1477
1557
  npmResults,