Skip to content

Commit

Permalink
indent align and review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
deyan-r committed Jan 15, 2025
1 parent 6c5ec4d commit aa38264
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 85 deletions.
18 changes: 16 additions & 2 deletions typescript/polyglotpkg/src/lib/file-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* 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 { readdirSync, realpathSync, statSync } from "fs";
import { join } from "path";

interface FindFilesOptions {
Expand All @@ -23,6 +23,20 @@ interface FindFilesOptions {
subPath?: string;
absolute?: boolean;
}
/**
* Returns list of files under directory tree matching search criteria
* Patterns may be:
* - RegExp
* - path/filename patterns, * and ** supported
* Options supported:
* - exclude - patterns for filenames to be excluded from result (optional)
* - maxDepth - maximum depth to search directory tree (default 10)
* - path - directory to search in (default .)
* - absolute - true: return full absolute path to file, false: return filename only (default false)
* @param {RegExp[] | string[]} patterns Filename patterns
* @param {FindFilesOptions} options Search options
* @returns {string[]} List of files found
*/
export function findFiles(patterns: RegExp[] | string[], options: FindFilesOptions = {}): string[] {
let result: string[] = [];

Expand All @@ -48,7 +62,7 @@ export function findFiles(patterns: RegExp[] | string[], options: FindFilesOptio
: directories.push(_path);
}

const someMatch = (fileName: string, pattern: string|RegExp) => {
const someMatch = (fileName: string, pattern: string | RegExp) => {
let regex: RegExp;
if (pattern instanceof RegExp) {
regex = pattern;
Expand Down
64 changes: 32 additions & 32 deletions typescript/polyglotpkg/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,20 @@ export function determineActionType(pkg: PlatformDefinition, actionType?: Action
export async function getProjectActions(options: PackagerOptions, actionType?: ActionType): Promise<ProjectActions> {

// Search for all polyglot.json files located in subfolders of src
const plg = findFiles([ 'src/**/polyglot.json' ], {
exclude: [ '**/node_modules/**' ],
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 = findFiles([ 'package.json' ], {
exclude: [ '**/node_modules/**' ],
path: options.workspace,
absolute: true
});
const pkg = findFiles([ 'package.json' ], {
exclude: [ '**/node_modules/**' ],
path: options.workspace,
absolute: true
});

if (pkg.length === 0) {
return [];
Expand Down Expand Up @@ -195,11 +195,11 @@ export async function createPackageJsonForABX(options: ActionOptions, isMixed: b
*/
export async function getActionManifest(projectPath: string): Promise<AbxActionDefinition | VroActionDefinition | null> {

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

if (pkg.length === 0) {
return null;
Expand All @@ -223,25 +223,25 @@ export function notUndefined<T>(x: T | undefined): x is T {
*/
export function run(cmd: string, args: Array<string> = [], cwd: string = process.cwd()): Promise<number> {
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' });
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);
});
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' });
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
6 changes: 3 additions & 3 deletions typescript/polyglotpkg/src/strategies/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export abstract class BaseStrategy implements IStrategy {
}
})

mkdirSync(path.dirname(this.options.bundle), { recursive: true });
mkdirSync(path.dirname(this.options.bundle), { recursive: true });
zip.writeZip(this.options.bundle);
this.logger.info(`Created ${this.options.bundle}`);
}
Expand All @@ -54,8 +54,8 @@ export abstract class BaseStrategy implements IStrategy {
const { files: filesToCopy, baseDir } = fileset;
for (const filePath of filesToCopy) {
const destPath = path.join(path.resolve(dest), path.relative(baseDir, filePath));
mkdirSync(path.dirname(destPath), { recursive: true });
copyFileSync(filePath, destPath);
mkdirSync(path.dirname(destPath), { recursive: true });
copyFileSync(filePath, destPath);
this.logger.debug(`Copied: ${filePath}`);
}
}
Expand Down
18 changes: 9 additions & 9 deletions typescript/polyglotpkg/src/strategies/nodejs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ export class NodejsStrategy extends BaseStrategy {
}
}

const filesToBundle = findFiles(patterns, {
path: workspaceFolderPath,
absolute: true
});

const depsToBundle = findFiles([ "**" ], {
path: this.DEPENDENCY_TEMP_DIR,
absolute: true
});
const filesToBundle = findFiles(patterns, {
path: workspaceFolderPath,
absolute: true
});

const depsToBundle = findFiles([ "**" ], {
path: this.DEPENDENCY_TEMP_DIR,
absolute: true
});

this.logger.info(`Packaging ${filesToBundle.length + depsToBundle.length} files into bundle ${this.options.bundle}...`);
const actionBase = polyglotJson.platform.base ? path.resolve(polyglotJson.platform.base) : this.options.outBase;
Expand Down
20 changes: 10 additions & 10 deletions typescript/polyglotpkg/src/strategies/powershell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,29 @@ export class PowershellStrategy extends BaseStrategy {
patterns.push(`${outDir}/**`);
}

const filesToBundle = findFiles(patterns, {
path: workspaceFolderPath,
absolute: true
});
const filesToBundle = findFiles(patterns, {
path: workspaceFolderPath,
absolute: true
});

const modulesToBundle = findFiles([ 'Modules' ], {
path: this.options.outBase, // use action specific out subfolder
absolute: true
});
const modulesToBundle = findFiles([ 'Modules' ], {
path: this.options.outBase, // use action specific out subfolder
absolute: true
});


this.logger.info(`Packaging ${filesToBundle.length} files into bundle ${this.options.bundle}...`);
const actionBase = polyglotJson.platform.base ? path.resolve(polyglotJson.platform.base) : this.options.outBase;
this.logger.info(`Action base: ${actionBase}`);
this.zipFiles([
this.zipFiles([
{ files: filesToBundle, baseDir: actionBase },
{ files: modulesToBundle, baseDir: actionBase }
]);
}

private async compile(source: string, destination: string) {
this.logger.info(`Compiling project...`);
cpSync(source, destination, { recursive: true });
cpSync(source, destination, { recursive: true });
this.logger.info(`Compilation complete`);
}

Expand Down
20 changes: 10 additions & 10 deletions typescript/polyglotpkg/src/strategies/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class PythonStrategy extends BaseStrategy {
});
}

mkdirSync(path.dirname(this.options.bundle), { recursive: true });
mkdirSync(path.dirname(this.options.bundle), { recursive: true });
zip.writeZip(this.options.bundle);
this.logger.info(`Created ${this.options.bundle}`);
}
Expand All @@ -83,7 +83,7 @@ export class PythonStrategy extends BaseStrategy {
* package project into bundle
*/
async packageProject() {
const polyglotJson = JSON.parse(readFileSync(this.options.polyglotJson).toString("utf8")) as PlatformDefinition;
const polyglotJson = JSON.parse(readFileSync(this.options.polyglotJson).toString("utf8")) as PlatformDefinition;

this.phaseCb(Events.COMPILE_START);
await this.compile(this.options.src, this.options.out);
Expand Down Expand Up @@ -112,15 +112,15 @@ export class PythonStrategy extends BaseStrategy {
patterns.push(`${outDir}/**`);
}

const filesToBundle = findFiles(patterns, {
path: workspaceFolderPath,
absolute: true
});
const filesToBundle = findFiles(patterns, {
path: workspaceFolderPath,
absolute: true
});

const depsToBundle = findFiles([ "**" ], {
path: this.DEPENDENCY_TEMP_DIR,
absolute: true
});
const depsToBundle = findFiles([ "**" ], {
path: this.DEPENDENCY_TEMP_DIR,
absolute: true
});

this.logger.info(`Packaging ${filesToBundle.length + depsToBundle.length} files into bundle ${this.options.bundle}...`);
const actionBase = path.resolve(path.join(this.options.outBase, polyglotJson.platform.base ? polyglotJson.platform.base : 'out'));
Expand Down
2 changes: 1 addition & 1 deletion typescript/polyglotpkg/src/vro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class VroTree {

const doc = createXML({ version: '1.0', encoding: 'UTF-8' }, content)
const xml = doc.end({ prettyPrint: true });
writeFileSync(path.join(this.treeDir, 'pom.xml'), xml);
writeFileSync(path.join(this.treeDir, 'pom.xml'), xml);
}

private generateAction(actionDefintion: VroActionDefinition) {
Expand Down
6 changes: 3 additions & 3 deletions typescript/vropkg/src/parse/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const xmlToAction = (file: string, bundlePath: string, name: string, comm
};
params.push(param);
} else if (element.type == "element" && element.name == "script") {
inline = getScriptInline(file, element, comment, false); // @TODO: check if lazy parameter is still relevant
inline = getScriptInline(file, element, comment);
} else if (element.type == "element" && element.name == "runtime") {
runtime = element.val;
} else if (element.type == "element" && element.name == "entry-point") {
Expand Down Expand Up @@ -131,7 +131,7 @@ export function getScriptRuntime(runtime: string): t.VroScriptRuntime {
return { lang: lang, version: langVersion != "" ? langVersion : defaultVersion };
}

function getScriptInline(file: string, element: xmldoc.XmlElement, comment: string, lazy: boolean): t.VroScriptInline {
function getScriptInline(file: string, element: xmldoc.XmlElement, comment: string): t.VroScriptInline {

let scriptSource: string = element.val;
let scriptStartLine: number = element.line;
Expand All @@ -152,7 +152,7 @@ function getScriptInline(file: string, element: xmldoc.XmlElement, comment: stri
scriptEndIndex = lastLine.length;
}
return {
actionSource: lazy ? null : scriptSource,
actionSource: scriptSource,
sourceFile: file,
sourceStartLine: scriptStartLine,
sourceStartIndex: scriptStartIndex,
Expand Down
4 changes: 1 addition & 3 deletions typescript/vropkg/src/serialize/js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import * as Path from "path";
import * as FileSystem from "fs-extra";
import * as XmlBuilder from "xmlbuilder2";
import * as unzipper from "unzipper";
import * as winston from "winston";
import * as packaging from "../packaging";
import { VroPackageMetadata, VroNativeElement, VroActionData, VroActionParameter, Lang, VroScriptBundle } from "../types";
import { exist, isDirectory } from "../util";
Expand Down Expand Up @@ -163,7 +161,7 @@ export class VroJsProjRealizer {
FileSystem.copy(bundle.contentPath, dest);
} else {
FileSystem.mkdirs(dest);
await packaging.extract(bundle.contentPath, dest);
await packaging.extract(bundle.contentPath, dest);
}
}

Expand Down
1 change: 1 addition & 0 deletions typescript/vropkg/src/serialize/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ const serializeTreeElement = async (context: any, element: t.VroNativeElement):
? element?.categoryPath.pop().split('.')
: element?.categoryPath;

// @NOTE: The below code will not support empty values for version, mimetype, and description attributes
if (element.type == t.VroElementType.ResourceElement && element.attributes["version"]) {
xInfo.ele("entry").att("key", "version").text(element.attributes["version"].toString());
}
Expand Down
12 changes: 0 additions & 12 deletions typescript/vrotest/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,3 @@ export async function withWorkingDir(dir: string, action: () => Promise<void>):
}
}
}

type CopyFilter = (fileName: string) => boolean;

/**
* Copy file or directory. Enables filtering.
* @param {string} path Copy source.
* @param {string} path Copy source.
* @return {boolean} Whether or not to copy the file.
*/
export async function copy(source: string, destination: string, filter?: CopyFilter) {

}

0 comments on commit aa38264

Please sign in to comment.