Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add importable scripts #7284

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down
52 changes: 52 additions & 0 deletions src/util/script-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* @flow */

import * as fs from './fs.js';
import type {Reporter} from '../reporters/index.js';

export async function checkImportScripts(scripts: any, reporter: Reporter): Promise<any> {
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;
}