@form8ion/javascript 7.2.1 → 7.2.4

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,28 @@ 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
- const eslintResults = await lift$4({projectRoot, configs: [...eslintConfigs || [], ...eslint?.configs || []]});
470
+ const eslintResults = await lift$4({
471
+ projectRoot,
472
+ configs: [...eslintConfigs || [], ...eslint?.configs || []],
473
+ buildDirectory
474
+ });
332
475
  const enhancerResults = await applyEnhancers({
333
476
  results,
334
- enhancers: [huskyPlugin, enginesEnhancer, coveragePlugin, commitConventionPlugin],
335
- options: {packageManager, projectRoot, vcs}
477
+ enhancers: [huskyPlugin, enginesEnhancer, coveragePlugin, commitConventionPlugin, dialects],
478
+ options: {packageManager, projectRoot, vcs, buildDirectory}
336
479
  });
337
480
 
338
481
  await liftPackage(
@@ -434,10 +577,10 @@ function validate(options) {
434
577
 
435
578
  function buildDialectChoices ({babelPreset, typescript}) {
436
579
  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'}] : []
580
+ {name: 'Common JS (no transpilation)', value: dialects$1.COMMON_JS, short: 'cjs'},
581
+ ...babelPreset ? [{name: 'Modern JavaScript (transpiled)', value: dialects$1.BABEL, short: 'modern'}] : [],
582
+ {name: 'ESM-only (no transpilation)', value: dialects$1.ESM, short: 'esm'},
583
+ ...typescript ? [{name: 'TypeScript', value: dialects$1.TYPESCRIPT, short: 'ts'}] : []
441
584
  ];
442
585
  }
443
586
 
@@ -620,64 +763,6 @@ async function prompt(
620
763
  };
621
764
  }
622
765
 
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
766
  function projectWillNotBeConsumed(projectType) {
682
767
  return projectTypes.APPLICATION === projectType || projectTypes.CLI === projectType;
683
768
  }
@@ -885,7 +970,7 @@ async function buildDetails ({
885
970
  dialect,
886
971
  decisions
887
972
  }) {
888
- if (dialects.COMMON_JS === dialect) return buildDetailsForCommonJsProject({projectRoot, projectName});
973
+ if (dialects$1.COMMON_JS === dialect) return buildDetailsForCommonJsProject({projectRoot, projectName});
889
974
 
890
975
  const chosenBundler = await chooseBundler({bundlers: packageBundlers, decisions});
891
976
 
@@ -942,14 +1027,14 @@ async function scaffoldPackageType ({
942
1027
  mergeIntoExistingPackageJson({
943
1028
  projectRoot,
944
1029
  config: {
945
- files: ['example.js', ...dialects.COMMON_JS === dialect ? ['index.js'] : ['lib/']],
1030
+ files: ['example.js', ...dialects$1.COMMON_JS === dialect ? ['index.js'] : ['lib/']],
946
1031
  publishConfig: {
947
1032
  access: 'Public' === visibility ? 'public' : 'restricted',
948
1033
  ...publishRegistry && {registry: publishRegistry}
949
1034
  },
950
1035
  sideEffects: false,
951
1036
  ...'Public' === visibility && {runkitExampleFilename: './example.js'},
952
- ...dialects.BABEL === dialect && {
1037
+ ...dialects$1.BABEL === dialect && {
953
1038
  main: './lib/index.js',
954
1039
  module: './lib/index.mjs',
955
1040
  exports: {
@@ -957,11 +1042,11 @@ async function scaffoldPackageType ({
957
1042
  import: './lib/index.mjs'
958
1043
  }
959
1044
  },
960
- ...dialects.ESM === dialect && {
1045
+ ...dialects$1.ESM === dialect && {
961
1046
  main: './lib/index.mjs',
962
1047
  exports: './lib/index.mjs'
963
1048
  },
964
- ...dialects.TYPESCRIPT === dialect && {
1049
+ ...dialects$1.TYPESCRIPT === dialect && {
965
1050
  main: './lib/index.js',
966
1051
  module: './lib/index.mjs',
967
1052
  types: './lib/index.d.ts',
@@ -1263,16 +1348,15 @@ async function scaffoldVerification({
1263
1348
  return deepmerge.all([testingResults, lintingResults, huskyResults]);
1264
1349
  }
1265
1350
 
1266
- async function scaffoldEslint ({config, projectRoot, buildDirectory, additionalConfiguration}) {
1351
+ async function scaffoldEslint ({config, projectRoot, additionalConfiguration}) {
1267
1352
  const {scope} = config;
1268
1353
  const {ignore} = additionalConfiguration;
1269
- const ignores = deepmerge(ignore, {directories: [`/${buildDirectory}/`]});
1270
1354
 
1271
- return scaffold$3({scope, projectRoot, ignore: {directories: ignores.directories}});
1355
+ return scaffold$3({scope, projectRoot, ignore});
1272
1356
  }
1273
1357
 
1274
1358
  async function scaffoldRemark ({config, projectRoot, projectType, vcs, dialect}) {
1275
- await write({
1359
+ await write$1({
1276
1360
  format: fileTypes.JSON,
1277
1361
  path: projectRoot,
1278
1362
  name: 'remark',
@@ -1304,7 +1388,7 @@ async function scaffoldRemark ({config, projectRoot, projectType, vcs, dialect})
1304
1388
  {
1305
1389
  ...projectTypes.PACKAGE === projectType && {
1306
1390
  devDependencies: ['remark-usage'],
1307
- ...dialects.COMMON_JS !== dialect && {scripts: {'pregenerate:md': 'run-s build'}}
1391
+ ...dialects$1.COMMON_JS !== dialect && {scripts: {'pregenerate:md': 'run-s build'}}
1308
1392
  }
1309
1393
  }
1310
1394
  );
@@ -1317,17 +1401,12 @@ async function scaffoldCodeStyle ({
1317
1401
  configs,
1318
1402
  vcs,
1319
1403
  configureLinting,
1320
- buildDirectory,
1321
1404
  eslint
1322
1405
  }) {
1323
1406
  return deepmerge.all(await Promise.all([
1324
- configs.eslint && configureLinting
1325
- && scaffoldEslint({
1326
- projectRoot,
1327
- config: configs.eslint,
1328
- buildDirectory,
1329
- additionalConfiguration: eslint
1330
- }),
1407
+ configs.eslint
1408
+ && configureLinting
1409
+ && scaffoldEslint({projectRoot, config: configs.eslint, additionalConfiguration: eslint}),
1331
1410
  scaffoldRemark({
1332
1411
  projectRoot,
1333
1412
  projectType,
@@ -1430,7 +1509,6 @@ async function scaffolder (options) {
1430
1509
  configs,
1431
1510
  projectRoot,
1432
1511
  projectType,
1433
- buildDirectory: projectTypeResults.buildDirectory,
1434
1512
  testFilenamePattern: verificationResults.testFilenamePattern
1435
1513
  }),
1436
1514
  scaffoldCodeStyle({
@@ -1440,7 +1518,6 @@ async function scaffolder (options) {
1440
1518
  configs,
1441
1519
  vcs,
1442
1520
  configureLinting,
1443
- buildDirectory: projectTypeResults.buildDirectory,
1444
1521
  eslint: verificationResults.eslint
1445
1522
  }),
1446
1523
  scaffoldProjectTypePlugin({
@@ -1470,8 +1547,8 @@ async function scaffolder (options) {
1470
1547
  scaffoldChoice(ciServices, ci, {projectRoot, vcs, visibility, projectType, projectName, nodeVersion, tests}),
1471
1548
  scaffold$5({projectRoot, projectType, configs, pathWithinParent})
1472
1549
  ])),
1473
- projectTypePluginResults,
1474
1550
  projectTypeResults,
1551
+ projectTypePluginResults,
1475
1552
  verificationResults,
1476
1553
  codeStyleResults,
1477
1554
  npmResults,