lodash#now TypeScript Examples
The following examples show how to use
lodash#now.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: pack-external-module.ts From malagu with MIT License | 4 votes |
/**
* We need a performant algorithm to install the packages for each single
* function (in case we package individually).
* (1) We fetch ALL packages needed by ALL functions in a first step
* and use this as a base npm checkout. The checkout will be done to a
* separate temporary directory with a package.json that contains everything.
* (2) For each single compile we copy the whole node_modules to the compile
* directory and create a (function) compile specific package.json and store
* it in the compile directory. Now we start npm again there, and npm will just
* remove the superfluous packages and optimize the remaining dependencies.
* This will utilize the npm cache at its best and give us the needed results
* and performance.
*/
export async function packExternalModules(context: ConfigurationContext, stats: Stats | undefined): Promise<void> {
const verbose = false;
const { cfg, pkg, runtime } = context;
const config = ConfigUtil.getMalaguConfig(cfg, BACKEND_TARGET);
const configuration = ConfigurationContext.getConfiguration(BACKEND_TARGET, context.configurations);
const includes = config.includeModules;
const packagerOptions = { nonInteractive: true, ignoreOptional: true, ...config.packagerOptions };
const scripts: any[] = packagerOptions.scripts || [];
if (isEmpty(includes) && includes !== true || !configuration) {
return;
}
const outputPath = configuration.output.get('path');
// Read plugin configuration
const packageForceIncludes = includes.forceInclude || [];
const packageForceExcludes = includes.forceExclude || [];
const packageForceIncludeAll = includes.forceIncludeAll;
const packagePath = includes.packagePath && join(process.cwd(), includes.packagePath) || join(process.cwd(), 'package.json');
const packageScripts = scripts.reduce((accumulator, script, index) => {
accumulator[`script${index}`] = script;
return accumulator;
},
{}
);
const packager = getPackager(context.cfg.rootConfig.packager, process.cwd());
const sectionNames = packager.copyPackageSectionNames;
const packageJson = await readJSON(packagePath);
if (packageForceIncludeAll) {
for (const d of Object.keys(packageJson.dependencies)) {
if (!packageForceIncludes.includes(d)) {
packageForceIncludes.push(d);
}
}
}
const packageSections = pick(packageJson, sectionNames);
if (!isEmpty(packageSections)) {
console.log(`Using package.json sections ${Object.keys(packageSections).join(', ')}`);
}
const dependencyGraph = await packager.getProdDependencies(1);
const problems = dependencyGraph.problems || [];
if (verbose && !isEmpty(problems)) {
console.log(`Ignoring ${problems.length} NPM errors:`);
problems.forEach((problem: any) => {
console.log(`=> ${problem}`);
});
}
// (1) Generate dependency composition
const externalModules = getExternalModules(stats).concat(packageForceIncludes.map((whitelistedPackage: string) => ({
external: whitelistedPackage
})));
const compositeModules = uniq(getProdModules(uniq(externalModules), packagePath, dependencyGraph, packageForceExcludes, runtime));
removeExcludedModules(compositeModules, packageForceExcludes, true);
if (isEmpty(compositeModules)) {
// The compiled code does not reference any external modules at all
console.log('No external modules needed');
return;
}
// (1.a) Install all needed modules
const compositeModulePath = outputPath;
const compositePackageJson = join(compositeModulePath, 'package.json');
// (1.a.1) Create a package.json
const compositePackage = defaults(
{
name: pkg.pkg.name,
version: pkg.pkg.version,
description: `Packaged externals for ${pkg.pkg.name}`,
private: true,
scripts: packageScripts
},
packageSections
);
const relPath = relative(compositeModulePath, dirname(packagePath));
addModulesToPackageJson(compositeModules, compositePackage, relPath);
writeJSONSync(compositePackageJson, compositePackage, { spaces: 2 });
// (1.a.2) Copy package-lock.json if it exists, to prevent unwanted upgrades
const packageLockPath = join(dirname(packagePath), packager.lockfileName);
const hasPackageLock = await pathExists(packageLockPath);
if (hasPackageLock) {
console.log('? malagu package lock found - Using locked versions');
try {
let packageLockFile = await packager.readLockfile(packageLockPath);
packageLockFile = packager.rebaseLockfile(relPath, packageLockFile);
packager.writeLockfile(join(compositeModulePath, packager.lockfileName), packageLockFile);
} catch (err) {
console.warn(`Warning: Could not read lock file: ${err.message}`);
}
}
const start = now();
for (const compositeModule of compositeModules) {
console.log(`? malagu external modules - ${compositeModule}`);
}
await packager.install(packagerOptions, compositeModulePath);
if (verbose) {
console.log(`Package took [${now() - start} ms]`);
}
// Prune extraneous packages - removes not needed ones
const startPrune = now();
await packager.prune(packagerOptions, compositeModulePath);
if (verbose) {
console.log(`Prune: ${compositeModulePath} [${now() - startPrune} ms]`);
}
// Prune extraneous packages - removes not needed ones
const startRunScripts = now();
await packager.runScripts(Object.keys(packageScripts), compositeModulePath);
if (verbose) {
console.log(`Run scripts: ${compositeModulePath} [${now() - startRunScripts} ms]`);
}
}