Switch back package downloaded to npm (#134)

This commit is contained in:
Timothee Guerin 2020-12-16 10:01:26 -08:00 коммит произвёл GitHub
Родитель 560ed23054
Коммит aa54aef187
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 108 добавлений и 118 удалений

3
.gitignore поставляемый
Просмотреть файл

@ -58,6 +58,9 @@ jspm_packages/
# Common toolchain intermediate files
temp
# MacOS
.DS_Store
# Rush files
common/temp/**
common/temp/**

Просмотреть файл

@ -1,13 +1,13 @@
{
"name": "@azure-tools/extension",
"version": "3.0.0",
"version": "3.1.0",
"patchOffset": 100,
"description": "Yarn-Based extension aquisition (for Azure Open Source Projects)",
"engines": {
"node": ">=10.12.0"
},
"main": "./dist/main.js",
"typings": "./dist/main.d.ts",
"main": "./dist/src/main.js",
"typings": "./dist/src/main.d.ts",
"scripts": {
"build": "tsc -p .",
"watch": "tsc -p . --watch",
@ -41,26 +41,28 @@
"@types/npm": "^2.0.28",
"npm": "5.6.0"
},
"devDependencies": {
"mocha-typescript": "1.1.17",
"@types/node": "12.7.2",
"@types/mocha": "5.2.5",
"@types/semver": "5.5.0",
"mocha": "5.2.0",
"@typescript-eslint/eslint-plugin": "~2.6.0",
"@typescript-eslint/parser": "~2.6.0",
"eslint": "~6.6.0",
"typescript": "~3.7.2",
"source-map-support": "0.5.13"
},
"dependencies": {
"@azure-tools/async-io": "~3.0.0",
"@azure-tools/eventing": "~3.0.0",
"@azure-tools/tasks": "~3.0.0",
"npm-package-arg": "6.1.0",
"semver": "^5.5.1",
"pacote": "9.2.3",
"npm-normalize-package-bin": "1.0.1",
"safe-buffer": "5.2.0"
"npm-package-arg": "6.1.0",
"pacote": "9.2.3",
"safe-buffer": "5.2.0",
"semver": "^5.5.1",
"yarn": "1.22.10",
"npm": "6.14.9"
},
"devDependencies": {
"@types/mocha": "5.2.5",
"@types/node": "12.7.2",
"@types/semver": "5.5.0",
"@typescript-eslint/eslint-plugin": "~2.6.0",
"@typescript-eslint/parser": "~2.6.0",
"eslint": "~6.6.0",
"mocha": "5.2.0",
"mocha-typescript": "1.1.17",
"source-map-support": "0.5.13",
"typescript": "~3.7.2"
}
}

Просмотреть файл

Просмотреть файл

@ -12,7 +12,7 @@ import { homedir, tmpdir } from 'os';
import * as pacote from 'pacote';
import { basename, delimiter, dirname, extname, isAbsolute, join, normalize, resolve } from 'path';
import * as semver from 'semver';
import { readFileSync } from 'fs';
import { npm } from './npm';
export async function updatePythonPath(command: Array<string>) {
const detect = quoteIfNecessary('import sys; print(sys.hexversion >= 0x03060000)');
@ -71,14 +71,14 @@ export async function updatePythonPath(command: Array<string>) {
switch (process.platform) {
case 'win32':
console.error('Python interpreter not found -- please install from Microsoft Store or from python.org (at least 3.6)');
break;
case 'darwin':
console.error('Python interpreter not found -- please install from homebrew (at least 3.6)');
break;
default:
console.error('Python interpreter not found -- please install python >= 3.6');
break;
}
throw new Error('Python interpreter not available.');
}
@ -328,86 +328,15 @@ export class LocalExtension extends Extension {
}
}
interface MoreOptions extends SpawnOptions {
onCreate?(cp: ChildProcess): void;
onStdOutData?(chunk: any): void;
onStdErrData?(chunk: any): void;
}
let _cli = '';
async function cli(): Promise<string> {
if (!_cli) {
for (const each of Object.keys(process.env)) {
if (each.startsWith('npm_')) {
delete process.env[each];
}
}
// if we can see the cli on disk, that's ok
const fname = resolve(`${__dirname}/../yarn/cli.js`);
if ((await isFile(fname))) {
_cli = fname;
}
else {
// otherwise, we might be in a 'static-linked' library and
// we should try to load it and put a copy in $tmp somewhere.
_cli = join(tmpdir(), 'yarn-cli.js');
// did we copy it already?
if ((await isFile(_cli))) {
return _cli;
}
// no, let's copy it now.
await writeFile(_cli, <string><any>readFileSync(fname));
}
}
return _cli;
}
function execute(command: string, cmdlineargs: Array<string>, options: MoreOptions): Promise<{ stdout: string; stderr: string; error: Error | null; code: number }> {
return new Promise((r, j) => {
const cp = spawn(command, cmdlineargs, { ...options, stdio: 'pipe' });
if (options.onCreate) {
options.onCreate(cp);
}
options.onStdOutData ? cp.stdout.on('data', options.onStdOutData) : cp;
options.onStdErrData ? cp.stderr.on('data', options.onStdErrData) : cp;
let err = '';
let out = '';
cp.stderr.on('data', (chunk) => {
err += chunk;
});
cp.stdout.on('data', (chunk) => {
out += chunk;
});
cp.on('close', (code, signal) => r({ stdout: out, stderr: err, error: code ? new Error('Process Failed.') : null, code }));
});
}
async function yarn(folder: string, cmd: string, ...args: Array<string>) {
const output = await execute(process.execPath, [
await cli(),
'--no-node-version-check',
'--no-lockfile',
'--json',
'--registry',
process.env.autorest_registry || 'https://registry.npmjs.org',
cmd,
...args
], { cwd: folder });
return output;
}
async function install(directory: string, ...pkgs: Array<string>) {
const output = await yarn(directory,
'add',
'--global-folder', directory.replace(/\\/g, '/'),
...pkgs);
const output = await npm(
directory,
"install",
"--save",
"--prefix",
directory.replace(/\\/g, "/"),
...pkgs
);
if (output.error) {
throw Error(`Failed to install package '${pkgs}' -- ${output.error}`);
}
@ -473,7 +402,7 @@ export class ExtensionManager {
// recreate the folder
await mkdir(this.installationPath);
await yarn(this.installationPath, 'cache', 'clean', '--force');
await npm(this.installationPath, 'cache', 'clean', '--force');
} catch (e) {
throw new ExtensionFolderLocked(this.installationPath);
} finally {
@ -487,7 +416,7 @@ export class ExtensionManager {
}
public async getPackageVersions(name: string): Promise<Array<string>> {
const versions = await yarn(process.cwd(), 'info', name, 'versions');
const versions = await npm(process.cwd(), 'view', name, 'versions', "--json");
return JSON.parse(versions.stdout).data.sort((b, a) => semver.compare(a, b));
}
@ -637,7 +566,6 @@ export class ExtensionManager {
// create the folder
await mkdir(extension.location);
// run YARN ADD for the package.
progress.NotifyMessage(`Installing ${pkg.name}, ${pkg.version}`);
const results = force ? install(extension.location, '--force', pkg.packageMetadata._resolved) : install(extension.location, pkg.packageMetadata._resolved);

64
extension/src/npm.ts Normal file
Просмотреть файл

@ -0,0 +1,64 @@
import { ChildProcess, spawn, SpawnOptions } from "child_process";
import { resolve } from "path";
const DEFAULT_NPM_REGISTRY = "https://registry.npmjs.org";
const getPathToNpmCli = () => {
return resolve(`${__dirname}/../../node_modules/npm/bin/npm-cli.js`);
};
export const npm = async (cwd: string, ...args: string[]) => {
const procArgs = [
getPathToNpmCli(),
"--no-shrinkwrap",
"--registry",
process.env.autorest_registry || DEFAULT_NPM_REGISTRY,
...args,
];
console.error("Args used", procArgs);
return await execute(process.execPath, procArgs, { cwd });
};
interface MoreOptions extends SpawnOptions {
onCreate?(cp: ChildProcess): void;
onStdOutData?(chunk: any): void;
onStdErrData?(chunk: any): void;
}
const execute = (
command: string,
cmdlineargs: Array<string>,
options: MoreOptions
): Promise<{
stdout: string;
stderr: string;
error: Error | null;
code: number;
}> => {
return new Promise((r, j) => {
const cp = spawn(command, cmdlineargs, { ...options, stdio: "pipe" });
if (options.onCreate) {
options.onCreate(cp);
}
options.onStdOutData ? cp.stdout.on("data", options.onStdOutData) : cp;
options.onStdErrData ? cp.stderr.on("data", options.onStdErrData) : cp;
let err = "";
let out = "";
cp.stderr.on("data", (chunk) => {
err += chunk;
});
cp.stdout.on("data", (chunk) => {
out += chunk;
});
cp.on("close", (code, signal) =>
r({
stdout: out,
stderr: err,
error: code ? new Error("Process Failed.") : null,
code,
})
);
});
};

Просмотреть файл

@ -4,7 +4,7 @@ import * as assert from 'assert';
import * as fs from 'fs';
import { suite, test } from 'mocha-typescript';
import * as os from 'os';
import { ExtensionManager, InvalidPackageIdentityException, UnresolvedPackageException } from '../main';
import { ExtensionManager, InvalidPackageIdentityException, UnresolvedPackageException } from '../src/main';
require('source-map-support').install();

Просмотреть файл

@ -3,15 +3,8 @@
"compilerOptions": {
"rootDir": ".",
"outDir": "dist",
"noImplicitAny": false,
"noImplicitAny": false
},
"include": [
"."
],
"exclude": [
"dist",
"resources",
"node_modules",
"**/*.d.ts"
]
"include": ["src/**/*.ts", "test/**/*.ts"],
"exclude": ["dist", "resources", "node_modules", "**/*.d.ts"]
}