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

Update dependencies to latest package versions for "typescript/polyglot" component. #7

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async function delay(func: () => void, ms: number = 1) {
setTimeout(() => {
try {
func();
resolve();
resolve(undefined);
} catch (e) {
reject(e.message);
}
Expand Down
1 change: 1 addition & 0 deletions typescript/polyglotpkg/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ node_modules/
**/.DS_Store
dist/
**/polyglot-cache/
test/unit/file-system/copy-files/destination
548 changes: 234 additions & 314 deletions typescript/polyglotpkg/package-lock.json

Large diffs are not rendered by default.

19 changes: 8 additions & 11 deletions typescript/polyglotpkg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"scripts": {
"start": "./bin/polyglotpkg",
"build": "tsc",
"build": "tsc --p tsconfig.build.json",
"clean": "run-script-os",
"clean:win32": "rmdir /S /Q dist || echo Nothing to clean",
"clean:default": "rm -Rf out dist",
Expand All @@ -28,8 +28,8 @@
"package"
],
"engines": {
"node": ">=16.15.1",
"npm": ">=6.14.0"
"node": ">=20.18.0",
"npm": ">=10.8.2"
},
"author": "VMware WWCoE",
"license": "VMware Confidential",
Expand All @@ -41,24 +41,21 @@
"dependencies": {
"adm-zip": "^0.5.16",
"command-line-args": "^6.0.1",
"fs-extra": "^9.0.1",
"globby": "^11.0.1",
"lodash": "^4.17.15",
"typescript": "^3.9.5",
"typescript": "^5.6.3",
"uuid": "^11.0.3",
"which": "^2.0.2",
"which": "^5.0.0",
"winston": "^3.2.1",
"xmlbuilder2": "^3.1.1"
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^14.0.13",
"@types/node": "^22.9.0",
"@types/adm-zip": "^0.5.6",
"@types/command-line-args": "^5.2.3",
"@types/fs-extra": "^9.0.1",
"@types/lodash": "4.14.182",
"@types/lodash": "4.17.13",
"@types/uuid": "^10.0.0",
"@types/which": "^1.3.2",
"@types/which": "^3.0.4",
"run-script-os": "^1.1.6",
"ts-node": "^10.9.2",
"mocha": "^10.2.0"
Expand Down
10 changes: 5 additions & 5 deletions typescript/polyglotpkg/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
* and interracting with the packager from command line.
*/
import path from 'path';
import fs from 'fs-extra';
import cmdArgs from "command-line-args";
import createLogger from './lib/logger';
import util from 'util';
import { Packager } from './packager';
import { existsSync, readFileSync } from 'fs';

interface CliInputs extends cmdArgs.CommandLineOptions {
/** verbose logging */
Expand Down Expand Up @@ -109,8 +109,8 @@ async function run(): Promise<void> {
*/
function printVersion() {
const packageJsonPath = path.join(__dirname, "../package.json");
if (fs.existsSync(packageJsonPath)) {
const packageConfig = fs.readJSONSync(packageJsonPath);
if (existsSync(packageJsonPath)) {
const packageConfig = JSON.parse(readFileSync(packageJsonPath).toString());
logger.info(`Version ${packageConfig.version}`);
}
}
Expand All @@ -120,8 +120,8 @@ function printVersion() {
*/
function printUsage() {
const usageFilePath = path.join(__dirname, "../Usage.txt");
if (fs.existsSync(usageFilePath)) {
const usageText = fs.readFileSync(usageFilePath).toString();
if (existsSync(usageFilePath)) {
const usageText = readFileSync(usageFilePath).toString();
logger.info(usageText);
}
}
Expand Down
98 changes: 98 additions & 0 deletions typescript/polyglotpkg/src/lib/file-system.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*-
* #%L
* polyglotpkg
* %%
* Copyright (C) 2023 - 2024 VMware
* %%
* Build Tools for VMware Aria
* Copyright 2023 VMware, Inc.
*
* This product is licensed to you under the BSD-2 license (the "License"). You may not use this product except in compliance with the BSD-2 License.
*
* This product may include a number of subcomponents with separate copyright notices and license terms. Your use of these subcomponents is subject to the terms and conditions of the subcomponent's license, as noted in the LICENSE file.
* #L%
*/
import { copyFileSync, mkdirSync, readdirSync, realpathSync, statSync } from "fs";
import { join } from "path";

interface FindFilesOptions {
exclude?: RegExp[] | string[];
maxDepth?: number;
currentDepth?: number;
path?: string;
subPath?: string;
absolute?: boolean;
}
export function findFiles(patterns: RegExp[] | string[], options: FindFilesOptions = {}): string[] {
let result: string[] = [];

const maxDepth = options.maxDepth || 10;
const currentDepth = options.currentDepth || 0;

if (currentDepth > maxDepth) {
return result;
}

const subPath = options.subPath ? join("/", options.subPath) : "";
const defaultPath = ".";
const path = options.path || defaultPath;
const exclude = options.exclude || [];
const workPath = join(path, subPath);
const files = [];
const directories = [];

for (const _path of readdirSync(workPath)) {
const fullPath = workPath === defaultPath ? _path : join(workPath, _path);
statSync(fullPath).isFile()
? files.push(fullPath)
: directories.push(_path);
}

const someMatch = (fileName: string, pattern: string|RegExp) => {
let regex: RegExp;
if (pattern instanceof RegExp) {
regex = pattern;
} else {
const single = "{single}";
const singleRegex = "[a-z0-9\-\_\.]*";
const double = "{double}";
const doubleRegex = "[a-z0-9\-\_\.\/]*";
if (pattern.indexOf("**") !== -1) {
pattern = pattern.split("**").join(double);
}
if (pattern.indexOf("*") !== -1) {
pattern = pattern.split("*").join(single);
}
pattern = pattern.split(single).join(singleRegex);
pattern = pattern.split(double).join(doubleRegex);
regex = new RegExp("^" + pattern + "$", "gi");
}
return regex.test(fileName);
};
const checkMatch = (fileName: string) =>
patterns.some(pattern => someMatch(fileName, pattern)
) && (
exclude.length === 0 ||
!exclude.some(pattern => someMatch(fileName, pattern))
);

for (const file of files) {
const shortFileName = file.substring(path.length + 1);
if (checkMatch(shortFileName)) {
result.push(!!options.absolute ? realpathSync(file) : shortFileName);
}
}

for (const dir of directories) {
result = [
...result,
...findFiles(patterns, {
...options,
currentDepth: currentDepth + 1,
subPath: join(subPath, dir)
})
];
}

return result;
}
74 changes: 41 additions & 33 deletions typescript/polyglotpkg/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
* #L%
*/

import globby from 'globby';
import fs from 'fs-extra';
import which from 'which';
import path from 'path';
import { spawn } from 'child_process';
Expand All @@ -29,6 +27,8 @@
PackagerOptions,
} from './model';
import createLogger from './logger';
import { readFileSync, writeFileSync } from 'fs';
import { findFiles } from './file-system';

const logger = createLogger();

Expand Down Expand Up @@ -106,23 +106,25 @@
export async function getProjectActions(options: PackagerOptions, actionType?: ActionType): Promise<ProjectActions> {

// Search for all polyglot.json files located in subfolders of src
const plg = await globby(['src/**/polyglot.json', '!**/node_modules/**'], {
cwd: options.workspace,
const plg = findFiles([ 'src/**/polyglot.json' ], {
exclude: [ '**/node_modules/**' ],
path: options.workspace,
absolute: true
});
});

if (plg.length === 0) {
// No polyglot.json found. Assuming legacy project.
// Locate package.json from project root
const pkg = await globby(['package.json', '!**/node_modules/**'], {
cwd: options.workspace,
absolute: true
});
const pkg = findFiles([ 'package.json' ], {
exclude: [ '**/node_modules/**' ],
path: options.workspace,
absolute: true
});

if (pkg.length === 0) {
return [];
}
const pkgObj = await fs.readJSONSync(pkg[0]);
const pkgObj = JSON.parse(readFileSync(pkg[0]).toString("utf8"));

// Set options for a legacy project
let projectAction: ActionOptions =
Expand Down Expand Up @@ -155,7 +157,7 @@
const outBasePath: string = path.join(options.workspace, 'out', actionFolder);
// To separate bundle.zip files of multiple actions they are created under project_root/dist/action_name/dist
const bundleZipPath: string = path.resolve('.', 'dist', actionFolder, 'dist', 'bundle.zip');
const pkgObj = await fs.readJSONSync(plg[i]);
const pkgObj = JSON.parse(readFileSync(plg[i]).toString("utf8"));
let projectAction: ActionOptions =
{
...options,
Expand All @@ -179,30 +181,31 @@
*/
export async function createPackageJsonForABX(options: ActionOptions, isMixed: boolean) {
const projectPkg = await getActionManifest(options.workspace) as PackageDefinition;
const polyglotPkg = isMixed && await fs.readJSONSync(options.polyglotJson) as AbxActionDefinition;
const polyglotPkg = isMixed && JSON.parse(readFileSync(options.polyglotJson).toString("utf8")) as AbxActionDefinition;
if (polyglotPkg && polyglotPkg.platform.action === "auto") {
polyglotPkg.platform.action = path.basename(options.actionBase);
}
const bundlePkg = isMixed ? { ...projectPkg, ...polyglotPkg } : projectPkg;
const actionDistPkgPath = path.join(options.workspace, 'dist', isMixed ? path.basename(options.actionBase) : "", 'package.json');
fs.writeJsonSync(actionDistPkgPath, bundlePkg);
writeFileSync(actionDistPkgPath, JSON.stringify(bundlePkg, null, 2));
}

/**
* Return the parsed content of the project's package.
*/
export async function getActionManifest(projectPath: string): Promise<AbxActionDefinition | VroActionDefinition | null> {

const pkg = await globby(['package.json', '!**/node_modules/**'], {
cwd: projectPath,
absolute: true
});
const pkg = findFiles([ "package.json" ], {
exclude: [ "**/node_modules/**" ],
path: projectPath,
absolute: true,
})

if (pkg.length === 0) {
return null;
}

const pkgObj = await fs.readJSONSync(pkg[0]);
const pkgObj = JSON.parse(readFileSync(pkg[0]).toString("utf8"));
return pkgObj;
}

Expand All @@ -219,21 +222,26 @@
* @param cmd
*/
export function run(cmd: string, args: Array<string> = [], cwd: string = process.cwd()): Promise<number> {
return new Promise((resolve, reject) => {
which(cmd, { all: true }, (err: Error | null, commandPath: string[] | undefined) => {
if (err || !commandPath) {
return reject(new Error(`Cannot find "${cmd}"`));
}
const proc = spawn(quoteString(commandPath[0]), args, { cwd, shell: true, stdio: 'inherit' });
proc.on('close', exitCode => {
if (exitCode !== 0) {
const commandLine = `${quoteString(commandPath[0])} ${args.join(' ')}`;
logger.error(`Error running command: ${commandLine}`);
return reject(new Error(`Exit code for ${cmd}: ${exitCode}`));
}
resolve(exitCode);
});
});
return new Promise(async (resolve, reject) => {
let err: any;
let commandPath: readonly string[] = [];
try {
commandPath = await which(cmd, { all: true, nothrow: false });
} catch(thrown) {
err = thrown;
}
if (err || !(commandPath && commandPath.length)) {
return reject(new Error(`Cannot find "${cmd}"`));
}
const proc = spawn(quoteString(commandPath[0]), args, { cwd, shell: true, stdio: 'inherit' });

Check warning

Code scanning / CodeQL

Unsafe shell command constructed from library input Medium

This shell argument which depends on
library input
is later used in a
shell command
.
This shell argument which depends on
library input
is later used in a
shell command
.
proc.on('close', exitCode => {
if (exitCode !== 0) {
const commandLine = `${quoteString(commandPath[0])} ${args.join(' ')}`;
logger.error(`Error running command: ${commandLine}`);
return reject(new Error(`Exit code for ${cmd}: ${exitCode}`));
}
resolve(exitCode);
});
});
}

Expand Down
Loading
Loading