From 908e228325ad47b3415240d15b6a7162a62417ec Mon Sep 17 00:00:00 2001 From: cvanem Date: Sun, 19 May 2019 03:23:29 -0600 Subject: [PATCH 1/4] Add importable scripts --- src/config.js | 2 ++ src/util/script-utils.js | 56 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/util/script-utils.js diff --git a/src/config.js b/src/config.js index 2fdf5ff162..fe902bf94c 100644 --- a/src/config.js +++ b/src/config.js @@ -5,6 +5,7 @@ import type {Reporter} from './reporters/index.js'; import type {Manifest, PackageRemote, WorkspacesManifestMap, WorkspacesConfig} from './types.js'; import type PackageReference from './package-reference.js'; import {execFromManifest} from './util/execute-lifecycle-script.js'; +import {checkImportScripts} from './util/script-utils'; import {resolveWithHome} from './util/path.js'; import {boolifyWithDefault} from './util/conversion.js'; import normalizeManifest from './util/normalize-manifest/index.js'; @@ -747,6 +748,7 @@ export default class Config { const data = await this.readJson(loc); data._registry = registry; data._loc = loc; + data.scripts = await checkImportScripts(data.scripts, this.reporter); return normalizeManifest(data, dir, this, isRoot); } else { return null; diff --git a/src/util/script-utils.js b/src/util/script-utils.js new file mode 100644 index 0000000000..2b8947e436 --- /dev/null +++ b/src/util/script-utils.js @@ -0,0 +1,56 @@ +/* @flow */ + +import * as fs from './fs.js'; +import type {Reporter} from '../reporters/index.js'; + +export async function checkImportScripts(scripts: string | Object, reporter: Reporter): Promise { + if (typeof scripts === 'string' && scripts.length > 0) { + if (await fs.exists(scripts)) { + const module = require(await fs.realpath(scripts)); + if (module && typeof module.scripts === 'object') { + scripts = iterateScripts(module.scripts, '', module.delimiter, reporter); + } else { + reporter && + reporter.warn( + `Invalid scripts module: ${scripts}. The module must be exported and include a root scripts object.`, + ); + } + } + } + return scripts; +} + +function iterateScripts(node: Object, path: string = '', delim: string = '.', reporter: Reporter): Object { + const scripts = {}; + + const addScript = (key, script) => { + scripts[key] && + reporter && + reporter.warn(`Duplicate script key detected: ${key}. Scripts should be structured to have unique keys.`); + scripts[key] = script; + }; + + if (node['script'] && typeof node['script'] === 'string') { + addScript(path, node['script']); // Add script, ignore other non object keys + } else { + Object.keys(node) + .filter(k => typeof node[k] === 'string') + .forEach(k => { + if (k === 'default') { + addScript(path, node[k]); + } else { + addScript([path, k].filter(Boolean).join(delim), node[k]); + } + }); + } + + // Process remaining object nodes + Object.keys(node) + .filter(k => typeof node[k] === 'object') + .forEach(k => { + const nodepath = k === 'default' ? path : [path, k].filter(Boolean).join(delim); + const iteratedScripts = iterateScripts(node[k], nodepath, delim, reporter); + Object.keys(iteratedScripts).forEach(k => addScript(k, iteratedScripts[k])); + }); + return scripts; +} From 206260030b931762f1646187a6f8a110b7d67b74 Mon Sep 17 00:00:00 2001 From: cvanem Date: Sun, 19 May 2019 05:02:09 -0600 Subject: [PATCH 2/4] fix lint errors --- src/util/script-utils.js | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/util/script-utils.js b/src/util/script-utils.js index 2b8947e436..9862f2458b 100644 --- a/src/util/script-utils.js +++ b/src/util/script-utils.js @@ -33,24 +33,20 @@ function iterateScripts(node: Object, path: string = '', delim: string = '.', re if (node['script'] && typeof node['script'] === 'string') { addScript(path, node['script']); // Add script, ignore other non object keys } else { - Object.keys(node) - .filter(k => typeof node[k] === 'string') - .forEach(k => { - if (k === 'default') { - addScript(path, node[k]); - } else { - addScript([path, k].filter(Boolean).join(delim), node[k]); - } - }); + Object.keys(node).filter(k => typeof node[k] === 'string').forEach(k => { + if (k === 'default') { + addScript(path, node[k]); + } else { + addScript([path, k].filter(Boolean).join(delim), node[k]); + } + }); } // Process remaining object nodes - Object.keys(node) - .filter(k => typeof node[k] === 'object') - .forEach(k => { - const nodepath = k === 'default' ? path : [path, k].filter(Boolean).join(delim); - const iteratedScripts = iterateScripts(node[k], nodepath, delim, reporter); - Object.keys(iteratedScripts).forEach(k => addScript(k, iteratedScripts[k])); - }); + Object.keys(node).filter(k => typeof node[k] === 'object').forEach(k => { + const nodepath = k === 'default' ? path : [path, k].filter(Boolean).join(delim); + const iteratedScripts = iterateScripts(node[k], nodepath, delim, reporter); + Object.keys(iteratedScripts).forEach(k => addScript(k, iteratedScripts[k])); + }); return scripts; } From 0247e2649abfdd93fa983e02e22f8b20763bd02f Mon Sep 17 00:00:00 2001 From: cvanem Date: Sun, 19 May 2019 05:14:25 -0600 Subject: [PATCH 3/4] Fix lint errors --- .flowconfig | 1 + src/util/script-utils.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.flowconfig b/.flowconfig index c777bb6ddb..1fec814349 100644 --- a/.flowconfig +++ b/.flowconfig @@ -19,6 +19,7 @@ untyped-type-import=warn [options] suppress_comment= \\(.\\|\n\\)*\\$FlowFixMe include_warnings=true +module.ignore_non_literal_requires=true [version] ^0.66.0 diff --git a/src/util/script-utils.js b/src/util/script-utils.js index 9862f2458b..eb41c736f7 100644 --- a/src/util/script-utils.js +++ b/src/util/script-utils.js @@ -3,7 +3,7 @@ import * as fs from './fs.js'; import type {Reporter} from '../reporters/index.js'; -export async function checkImportScripts(scripts: string | Object, reporter: Reporter): Promise { +export async function checkImportScripts(scripts: string || Object, reporter: Reporter): Promise { if (typeof scripts === 'string' && scripts.length > 0) { if (await fs.exists(scripts)) { const module = require(await fs.realpath(scripts)); From ffb5952de94d52eb51d39e0373259a73047eba99 Mon Sep 17 00:00:00 2001 From: cvanem Date: Sun, 19 May 2019 05:19:07 -0600 Subject: [PATCH 4/4] Fix lint errors --- src/util/script-utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/script-utils.js b/src/util/script-utils.js index eb41c736f7..c50ef1f93a 100644 --- a/src/util/script-utils.js +++ b/src/util/script-utils.js @@ -3,7 +3,7 @@ import * as fs from './fs.js'; import type {Reporter} from '../reporters/index.js'; -export async function checkImportScripts(scripts: string || Object, reporter: Reporter): Promise { +export async function checkImportScripts(scripts: any, reporter: Reporter): Promise { if (typeof scripts === 'string' && scripts.length > 0) { if (await fs.exists(scripts)) { const module = require(await fs.realpath(scripts));