Skip to content

Commit 56b62ff

Browse files
committed
Fix package deploy to not depend on rush
1 parent 9083c5d commit 56b62ff

5 files changed

Lines changed: 34 additions & 172 deletions

File tree

extensions/ql-vscode/gulpfile.ts/deploy.ts

Lines changed: 25 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -1,197 +1,68 @@
11
import * as fs from 'fs-extra';
22
import * as jsonc from 'jsonc-parser';
3-
import { IPackageJson } from '@microsoft/node-core-library';
43
import * as path from 'path';
5-
import { getRushContext, RushContext } from './rush';
6-
import * as packlist from 'npm-packlist';
7-
import * as glob from 'glob-promise';
8-
import * as cpp from 'child-process-promise';
9-
10-
interface PackageInfo {
11-
name: string;
12-
version: string;
13-
sourcePath: string;
14-
files: string[];
15-
dependencies: PackageInfo[];
16-
isRoot?: boolean;
17-
copied?: boolean;
18-
}
19-
20-
async function copyPackage(packageFiles: PackageInfo, destPath: string): Promise<void> {
21-
for (const file of packageFiles.files) {
22-
const sourceFilePath = path.resolve(packageFiles.sourcePath, file);
23-
const destFilePath = path.resolve(destPath, file);
24-
if (packageFiles.isRoot && (file === 'package.json')) {
25-
// For non-release builds, we tweak the version number of the extension to add a prerelease
26-
// suffix. Rather than just copying `package.json`, we'll parse the original copy, update the
27-
// `version` property, and write it out to the new location.
28-
const packageJson = jsonc.parse((await fs.readFile(sourceFilePath)).toString());
29-
packageJson.version = packageFiles.version;
30-
await fs.writeFile(destFilePath, JSON.stringify(packageJson));
31-
}
32-
else {
33-
await fs.copy(sourceFilePath, destFilePath);
34-
}
35-
}
36-
}
374

385
export interface DeployedPackage {
396
distPath: string;
407
name: string;
418
version: string;
429
}
4310

44-
class PackageMap {
45-
private map = new Map<string, Map<string, PackageInfo>>();
46-
47-
public getPackageInfo(name: string, version: string): PackageInfo | undefined {
48-
const versionMap = this.map.get(name);
49-
if (versionMap === undefined) {
50-
return undefined;
51-
}
52-
53-
return versionMap.get(version);
54-
}
55-
56-
public addPackageInfo(pkg: PackageInfo): void {
57-
if (this.getPackageInfo(pkg.name, pkg.version)) {
58-
throw new Error(`Attempt to add duplicate package '${pkg.name}@${pkg.version}'.`);
59-
}
60-
61-
let versionMap = this.map.get(pkg.name);
62-
if (versionMap === undefined) {
63-
versionMap = new Map<string, PackageInfo>();
64-
this.map.set(pkg.name, versionMap);
65-
}
66-
67-
versionMap.set(pkg.version, pkg);
68-
}
69-
70-
public hasMultipleVersions(name: string): boolean {
71-
return this.map.get(name)!.size > 1;
72-
}
73-
}
74-
75-
async function collectPackages(context: RushContext, name: string, version: string,
76-
pkgs: PackageMap): Promise<PackageInfo> {
77-
78-
let pkg = pkgs.getPackageInfo(name, version);
79-
if (!pkg) {
80-
const info = await context.getPackageInfo(name, version);
81-
82-
let files: string[];
83-
if (info.isLocal) {
84-
// For local packages, use `packlist` to get the list of files that npm would have packed
85-
// into the tarball.
86-
files = packlist.sync({ path: info.path });
87-
}
88-
else {
89-
// For non-local packages, just copy everything.
90-
files = await glob('**/*', {
91-
nodir: true,
92-
cwd: info.path
93-
});
94-
}
95-
96-
pkg = {
97-
name: name,
98-
version: version,
99-
sourcePath: info.path,
100-
files: files,
101-
dependencies: []
102-
};
103-
104-
pkgs.addPackageInfo(pkg);
105-
106-
for (const dependencyName of info.dependencies.keys()) {
107-
const dependencyVersion = info.dependencies.get(dependencyName)!;
108-
109-
const dependencyPackage = await collectPackages(context, dependencyName, dependencyVersion, pkgs);
110-
pkg.dependencies.push(dependencyPackage);
111-
}
112-
}
113-
114-
return pkg;
115-
}
116-
117-
async function copyPackageAndModules(pkg: PackageInfo, pkgs: PackageMap, destPath: string,
118-
rootNodeModulesPath: string): Promise<void> {
119-
120-
let destPackagePath: string;
121-
if (pkgs.hasMultipleVersions(pkg.name) || pkg.isRoot) {
122-
// Copy as a nested package, and let `npm dedupe` fix it up later if possible.
123-
destPackagePath = path.join(destPath, pkg.name);
124-
}
125-
else {
126-
// Copy to the root `node_modules` directory.
127-
if (pkg.copied) {
128-
return;
129-
}
130-
pkg.copied = true;
131-
destPackagePath = path.join(rootNodeModulesPath, pkg.name);
132-
}
133-
134-
await copyPackage(pkg, destPackagePath);
135-
const nodeModulesPath = path.join(destPackagePath, 'node_modules');
136-
for (const dependencyPkg of pkg.dependencies) {
137-
await copyPackageAndModules(dependencyPkg, pkgs, nodeModulesPath, rootNodeModulesPath);
11+
const packageFiles = [
12+
'.vscodeignore',
13+
'CHANGELOG.md',
14+
'README.md',
15+
'language-configuration.json',
16+
'media',
17+
'node_modules',
18+
'out'
19+
];
20+
21+
async function copyPackage(sourcePath: string, destPath: string): Promise<void> {
22+
for (const file of packageFiles) {
23+
console.log(`copying ${path.resolve(sourcePath, file)} to ${path.resolve(destPath, file)}`);
24+
await fs.copy(path.resolve(sourcePath, file), path.resolve(destPath, file));
13825
}
13926
}
14027

14128
export async function deployPackage(packageJsonPath: string): Promise<DeployedPackage> {
14229
try {
143-
const context = await getRushContext(path.dirname(packageJsonPath));
144-
145-
const rootPackage: IPackageJson = jsonc.parse(await fs.readFile(packageJsonPath, 'utf8'));
30+
const packageJson: any = jsonc.parse(await fs.readFile(packageJsonPath, 'utf8'));
14631

14732
// Default to development build; use flag --release to indicate release build.
14833
const isDevBuild = !process.argv.includes('--release');
149-
const distDir = path.join(context.rushConfig.rushJsonFolder, 'dist');
34+
const distDir = path.join(__dirname, '../../../dist');
15035
await fs.mkdirs(distDir);
15136

15237
if (isDevBuild) {
15338
// NOTE: rootPackage.name had better not have any regex metacharacters
154-
const oldDevBuildPattern = new RegExp('^' + rootPackage.name + '[^/]+-dev[0-9.]+\\.vsix$');
39+
const oldDevBuildPattern = new RegExp('^' + packageJson.name + '[^/]+-dev[0-9.]+\\.vsix$');
15540
// Dev package filenames are of the form
15641
// vscode-codeql-0.0.1-dev.2019.9.27.19.55.20.vsix
15742
(await fs.readdir(distDir)).filter(name => name.match(oldDevBuildPattern)).map(build => {
15843
console.log(`Deleting old dev build ${build}...`);
15944
fs.unlinkSync(path.join(distDir, build));
16045
});
16146
const now = new Date();
162-
rootPackage.version = rootPackage.version +
47+
packageJson.version = packageJson.version +
16348
`-dev.${now.getUTCFullYear()}.${now.getUTCMonth() + 1}.${now.getUTCDate()}` +
16449
`.${now.getUTCHours()}.${now.getUTCMinutes()}.${now.getUTCSeconds()}`;
16550
}
16651

167-
const distPath = path.join(distDir, rootPackage.name);
52+
const distPath = path.join(distDir, packageJson.name);
16853
await fs.remove(distPath);
16954
await fs.mkdirs(distPath);
17055

171-
console.log(`Gathering transitive dependencies of package '${rootPackage.name}'...`);
172-
const pkgs = new PackageMap();
173-
const rootPkg = await collectPackages(context, rootPackage.name, rootPackage.version, pkgs);
174-
rootPkg.isRoot = true;
175-
176-
console.log(`Copying package '${rootPackage.name}' and its dependencies to '${distPath}'...`);
177-
await copyPackageAndModules(rootPkg, pkgs, path.dirname(distPath), path.join(distPath, 'node_modules'));
178-
await fs.copy(path.resolve(rootPkg.sourcePath, '.vscodeignore'), path.resolve(distPath, '.vscodeignore'));
56+
await fs.writeFile(path.join(distPath, 'package.json'), JSON.stringify(packageJson, null, 2));
17957

180-
console.log(`Deduplicating dependencies of package '${rootPackage.name}'...`);
181-
// We create a temporary `package-lock.json` file just to prevent `npm ls` from printing out the
182-
// message that it created a package-lock.json.
183-
const packageLockPath = path.join(distPath, 'package-lock.json');
184-
await fs.writeFile(packageLockPath, '{}');
185-
await cpp.spawn('npm', ['dedupe'], {
186-
cwd: distPath,
187-
stdio: 'inherit'
188-
});
189-
await fs.unlink(packageLockPath);
58+
const sourcePath = path.join(__dirname, '..');
59+
console.log(`Copying package '${packageJson.name}' and its dependencies to '${distPath}'...`);
60+
await copyPackage(sourcePath, distPath);
19061

19162
return {
19263
distPath: distPath,
193-
name: rootPackage.name,
194-
version: rootPackage.version
64+
name: packageJson.name,
65+
version: packageJson.version
19566
};
19667
}
19768
catch (e) {

extensions/ql-vscode/gulpfile.ts/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { compileTypeScript, watchTypeScript, copyViewCss } from './typescript';
33
import { compileTextMateGrammar } from './textmate';
44
import { copyTestData } from './tests';
55
import { compileView } from './webpack';
6+
import { packageExtension } from './package';
67

78
export const buildWithoutPackage = gulp.parallel(compileTypeScript, compileTextMateGrammar, compileView, copyTestData, copyViewCss);
89
export { compileTextMateGrammar, watchTypeScript, compileTypeScript };
9-
// exports.default = gulp.series(exports.buildWithoutPackage, packageExtension);
10+
exports.default = gulp.series(exports.buildWithoutPackage, packageExtension);

extensions/ql-vscode/gulpfile.ts/pnpm.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

extensions/ql-vscode/package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extensions/ql-vscode/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,7 @@
652652
"gulp-sourcemaps": "^2.6.5",
653653
"gulp-typescript": "^5.0.1",
654654
"husky": "~4.2.5",
655+
"jsonc-parser": "^2.3.0",
655656
"lint-staged": "~10.2.2",
656657
"mocha": "~6.2.1",
657658
"mocha-sinon": "~2.1.0",

0 commit comments

Comments
 (0)