move build/dependencies.js to typescript

This commit is contained in:
João Moreno 2021-01-21 09:32:06 +01:00
parent 7e2d8b48e3
commit 9cfba54681
No known key found for this signature in database
GPG key ID: 896B853774D1A575
5 changed files with 89 additions and 17 deletions

View file

@ -9,7 +9,7 @@ const es = require("event-stream");
const vfs = require("vinyl-fs");
const util = require("../lib/util");
// @ts-ignore
const deps = require("../dependencies");
const deps = require("../lib/dependencies");
const azure = require('gulp-azure-storage');
const root = path.dirname(path.dirname(__dirname));
const commit = util.getVersion(root);

View file

@ -11,7 +11,7 @@ import * as Vinyl from 'vinyl';
import * as vfs from 'vinyl-fs';
import * as util from '../lib/util';
// @ts-ignore
import * as deps from '../dependencies';
import * as deps from '../lib/dependencies';
const azure = require('gulp-azure-storage');
const root = path.dirname(path.dirname(__dirname));

View file

@ -26,7 +26,7 @@ const packageJson = require('../package.json');
const product = require('../product.json');
const crypto = require('crypto');
const i18n = require('./lib/i18n');
const { getProductionDependencies } = require('./dependencies');
const { getProductionDependencies } = require('./lib/dependencies');
const { config } = require('./lib/electron');
const createAsar = require('./lib/asar').createAsar;
const minimist = require('minimist');

60
build/lib/dependencies.js Normal file
View file

@ -0,0 +1,60 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
exports.getProductionDependencies = void 0;
const path = require("path");
const cp = require("child_process");
const _ = require("underscore");
const parseSemver = require('parse-semver');
function asYarnDependency(prefix, tree) {
let parseResult;
try {
parseResult = parseSemver(tree.name);
}
catch (err) {
err.message += `: ${tree.name}`;
console.warn(`Could not parse semver: ${tree.name}`);
return null;
}
// not an actual dependency in disk
if (parseResult.version !== parseResult.range) {
return null;
}
const name = parseResult.name;
const version = parseResult.version;
const dependencyPath = path.join(prefix, name);
const children = [];
for (const child of (tree.children || [])) {
const dep = asYarnDependency(path.join(prefix, name, 'node_modules'), child);
if (dep) {
children.push(dep);
}
}
return { name, version, path: dependencyPath, children };
}
function getYarnProductionDependencies(cwd) {
const raw = cp.execSync('yarn list --json', { cwd, encoding: 'utf8', env: Object.assign(Object.assign({}, process.env), { NODE_ENV: 'production' }), stdio: [null, null, 'inherit'] });
const match = /^{"type":"tree".*$/m.exec(raw);
if (!match || match.length !== 1) {
throw new Error('Could not parse result of `yarn list --json`');
}
const trees = JSON.parse(match[0]).data.trees;
return trees
.map(tree => asYarnDependency(path.join(cwd, 'node_modules'), tree))
.filter((dep) => !!dep);
}
function getProductionDependencies(cwd) {
const result = [];
const deps = getYarnProductionDependencies(cwd);
const flatten = (dep) => { result.push({ name: dep.name, version: dep.version, path: dep.path }); dep.children.forEach(flatten); };
deps.forEach(flatten);
return _.uniq(result);
}
exports.getProductionDependencies = getProductionDependencies;
if (require.main === module) {
const root = path.dirname(path.dirname(__dirname));
console.log(JSON.stringify(getProductionDependencies(root), null, ' '));
}

View file

@ -5,12 +5,27 @@
'use strict';
const path = require('path');
import * as path from 'path';
import * as cp from 'child_process';
import * as _ from 'underscore';
const parseSemver = require('parse-semver');
const cp = require('child_process');
const _ = require('underscore');
function asYarnDependency(prefix, tree) {
interface Tree {
readonly name: string;
readonly children?: Tree[];
}
interface FlatDependency {
readonly name: string;
readonly version: string;
readonly path: string;
}
interface Dependency extends FlatDependency {
readonly children: Dependency[];
}
function asYarnDependency(prefix: string, tree: Tree): Dependency | null {
let parseResult;
try {
@ -42,7 +57,7 @@ function asYarnDependency(prefix, tree) {
return { name, version, path: dependencyPath, children };
}
function getYarnProductionDependencies(cwd) {
function getYarnProductionDependencies(cwd: string): Dependency[] {
const raw = cp.execSync('yarn list --json', { cwd, encoding: 'utf8', env: { ...process.env, NODE_ENV: 'production' }, stdio: [null, null, 'inherit'] });
const match = /^{"type":"tree".*$/m.exec(raw);
@ -50,25 +65,22 @@ function getYarnProductionDependencies(cwd) {
throw new Error('Could not parse result of `yarn list --json`');
}
const trees = JSON.parse(match[0]).data.trees;
const trees = JSON.parse(match[0]).data.trees as Tree[];
return trees
.map(tree => asYarnDependency(path.join(cwd, 'node_modules'), tree))
.filter(dep => !!dep);
.filter<Dependency>((dep): dep is Dependency => !!dep);
}
function getProductionDependencies(cwd) {
const result = [];
export function getProductionDependencies(cwd: string): FlatDependency[] {
const result: FlatDependency[] = [];
const deps = getYarnProductionDependencies(cwd);
const flatten = dep => { result.push({ name: dep.name, version: dep.version, path: dep.path }); dep.children.forEach(flatten); };
const flatten = (dep: Dependency) => { result.push({ name: dep.name, version: dep.version, path: dep.path }); dep.children.forEach(flatten); };
deps.forEach(flatten);
return _.uniq(result);
}
module.exports.getProductionDependencies = getProductionDependencies;
if (require.main === module) {
const root = path.dirname(__dirname);
const root = path.dirname(path.dirname(__dirname));
console.log(JSON.stringify(getProductionDependencies(root), null, ' '));
}