Skip to content

Commit

Permalink
refactor(sys-node): update sys node and node workers
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Jun 30, 2020
1 parent 214688c commit 9d27e2d
Show file tree
Hide file tree
Showing 19 changed files with 411 additions and 768 deletions.
1 change: 1 addition & 0 deletions src/sys/node/bundles/glob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('glob');
3 changes: 3 additions & 0 deletions src/sys/node/bundles/prompts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// we already know node will be greater than 8.6.0
// https://github.com/terkelg/prompts/blob/master/index.js
module.exports = require('prompts/lib/index.js');
5 changes: 5 additions & 0 deletions src/sys/node/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export { createNodeLogger } from './node-logger';
export { createNodeSysWithWatch } from './node-sys-watch';
export { createNodeSys } from './node-sys';
export { checkVersion } from './node-stencil-version-checker';
export { setupNodeProcess } from './node-setup-process';
3 changes: 2 additions & 1 deletion src/sys/node/node-copy-tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ const IGNORE = ['.ds_store', '.gitignore', 'desktop.ini', 'thumbs.db'];

export function asyncGlob(pattern: string, opts: any) {
return new Promise<string[]>((resolve, reject) => {
glob(pattern, opts, (err: any, files: string[]) => {
const g: typeof glob = (glob as any).glob;
g(pattern, opts, (err: any, files: string[]) => {
if (err) {
reject(err);
} else {
Expand Down
1 change: 1 addition & 0 deletions src/sys/node/node-fs-promisify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import { promisify } from 'util';
export const copyFile = promisify(fs.copyFile);
export const mkdir = promisify(fs.mkdir);
export const readdir = promisify(fs.readdir);
export const readFile = promisify(fs.readFile);
export const stat = promisify(fs.stat);
36 changes: 11 additions & 25 deletions src/sys/node/node-lazy-require.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import * as d from '../../declarations';
import { dirname } from 'path';
import path from 'path';
import { NodeResolveModule } from './node-resolve-module';
import { readFile } from 'graceful-fs';
import { readFile } from './node-fs-promisify';
import { SpawnOptions, spawn } from 'child_process';
import semiver from 'semiver';

export class NodeLazyRequire implements d.LazyRequire {
private moduleData = new Map<string, { fromDir: string; modulePath: string }>();

constructor(
private nodeResolveModule: NodeResolveModule,
private lazyDependencies: {[dep: string]: [string, string]}
) {}
constructor(private nodeResolveModule: NodeResolveModule, private lazyDependencies: { [dep: string]: [string, string] }) {}

async ensure(logger: d.Logger, fromDir: string, ensureModuleIds: string[]) {
const depsToInstall: DepToInstall[] = [];
Expand All @@ -35,7 +32,7 @@ export class NodeLazyRequire implements d.LazyRequire {
if (semiver(installedPkgJson.version, minVersion) >= 0) {
this.moduleData.set(ensureModuleId, {
fromDir: fromDir,
modulePath: dirname(resolvedPkgJsonPath),
modulePath: path.dirname(resolvedPkgJsonPath),
});
return;
}
Expand Down Expand Up @@ -93,7 +90,7 @@ export class NodeLazyRequire implements d.LazyRequire {

if (!moduleData.modulePath) {
const modulePkgJsonPath = this.nodeResolveModule.resolveModule(moduleData.fromDir, moduleId);
moduleData.modulePath = dirname(modulePkgJsonPath);
moduleData.modulePath = path.dirname(modulePkgJsonPath);
this.moduleData.set(moduleId, moduleData);
}

Expand All @@ -109,7 +106,7 @@ export class NodeLazyRequire implements d.LazyRequire {

if (!moduleData.modulePath) {
const modulePkgJsonPath = this.nodeResolveModule.resolveModule(moduleData.fromDir, moduleId);
moduleData.modulePath = dirname(modulePkgJsonPath);
moduleData.modulePath = path.dirname(modulePkgJsonPath);
this.moduleData.set(moduleId, moduleData);
}

Expand All @@ -130,7 +127,7 @@ function npmInstall(logger: d.Logger, fromDir: string, moduleIds: string[]) {
};
opts.env.NODE_ENV = 'development';

if (logger.level === 'debug') {
if (logger.getLevel() === 'debug') {
args.push('--verbose');
}

Expand All @@ -156,7 +153,7 @@ function npmInstall(logger: d.Logger, fromDir: string, moduleIds: string[]) {
}

childProcess.once('exit', exitCode => {
if (logger.level === 'debug') {
if (logger.getLevel() === 'debug') {
logger.debug(`${cmd}, exit ${exitCode}`);
}

Expand All @@ -169,20 +166,9 @@ function npmInstall(logger: d.Logger, fromDir: string, moduleIds: string[]) {
});
}

function readPackageJson(pkgJsonPath: string) {
return new Promise<d.PackageJsonData>((resolve, reject) => {
readFile(pkgJsonPath, 'utf8', (err, data) => {
if (err) {
reject(err);
} else {
try {
resolve(JSON.parse(data));
} catch (e) {
reject(e);
}
}
});
});
async function readPackageJson(pkgJsonPath: string) {
const data = await readFile(pkgJsonPath, 'utf8');
return JSON.parse(data) as d.PackageJsonData;
}

interface DepToInstall {
Expand Down
6 changes: 6 additions & 0 deletions src/sys/node/node-load-typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { requireFunc } from '@utils';

export const nodeLoadTypeScript = (typeScriptPath: string) => {
const nodeModuleId = typeScriptPath || 'typescript';
return requireFunc(nodeModuleId);
};
Loading

0 comments on commit 9d27e2d

Please sign in to comment.