From cde9845e9597e4bd16c71161adcaaabfea217d62 Mon Sep 17 00:00:00 2001 From: develar Date: Sat, 17 Mar 2018 19:02:34 +0100 Subject: [PATCH] feat: Support for sibling packages (lerna + yarn workspaces) Close #2222 --- package.json | 6 +- .../src/util/AppFileCopierHelper.ts | 18 ++++- .../src/util/packageDependencies.ts | 70 ++++++++++++------- yarn.lock | 12 ++-- 4 files changed, 69 insertions(+), 37 deletions(-) diff --git a/package.json b/package.json index d1967d8e5a6..4142fbff702 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "docker-images": "docker/build.sh", "update-deps": "npm-check-updates -a -x gitbook-plugin-github,chalk && node ./scripts/update-deps.js", "set-versions": "node test/out/helpers/setVersions.js", - "release": "yarn set-versions && yarn compile && sh ./__publish.sh && conventional-changelog -p angular -i CHANGELOG.md -s", + "release": "BABEL_ENV=production yarn set-versions && yarn compile && sh ./__publish.sh && conventional-changelog -p angular -i CHANGELOG.md -s", "schema": "typescript-json-schema packages/electron-builder-lib/tsconfig.json Configuration --out packages/electron-builder-lib/scheme.json --noExtraProps --useTypeOfKeyword --strictNullChecks --titles --required", "jsdoc": "ts2jsdoc packages/builder-util-runtime packages/electron-updater packages/builder-util packages/electron-builder-lib packages/electron-builder packages/electron-publish", "jsdoc2md": "node scripts/jsdoc2md.js", @@ -83,7 +83,7 @@ "@types/semver": "^5.5.0", "@types/source-map-support": "^0.4.0", "@types/stat-mode": "^0.2.0", - "babel-preset-ts-node6-bluebird": "^2.0.1", + "babel-preset-ts-node6-bluebird": "^2.0.3", "convert-source-map": "^1.5.1", "decompress-zip": "^0.3.0", "depcheck": "^0.6.9", @@ -100,7 +100,7 @@ "jest-junit": "^3.6.0", "jsdoc-to-markdown": "^4.0.1", "path-sort": "^0.1.0", - "ts-babel": "^5.0.1", + "ts-babel": "^5.0.2", "ts-jsdoc": "^3.0.0", "tslint": "^5.9.1", "typescript": "2.7.2", diff --git a/packages/electron-builder-lib/src/util/AppFileCopierHelper.ts b/packages/electron-builder-lib/src/util/AppFileCopierHelper.ts index d2ce568f63a..bb7209420ce 100644 --- a/packages/electron-builder-lib/src/util/AppFileCopierHelper.ts +++ b/packages/electron-builder-lib/src/util/AppFileCopierHelper.ts @@ -79,7 +79,7 @@ export async function computeFileSets(matchers: Array, transformer: return false }, CONCURRENCY) - fileSets.push({src: matcher.from, files, metadata, transformedFiles, destination: matcher.to}) + fileSets.push(validateFileSet({src: matcher.from, files, metadata, transformedFiles, destination: matcher.to})) } if (isElectronCompile) { @@ -92,11 +92,23 @@ export async function computeFileSets(matchers: Array, transformer: return fileSets } +function validateFileSet(fileSet: ResolvedFileSet): ResolvedFileSet { + if (fileSet.src == null || fileSet.src.length === 0) { + throw new Error("fileset src is empty") + } + return fileSet +} + async function copyHoistedNodeModules(packager: Packager, mainMatcher: FileMatcher): Promise> { const productionDeps = await packager.productionDeps.value const rootPathToCopier = new Map>() for (const dep of productionDeps) { - const root = dep.path.substring(0, dep.path.indexOf(NODE_MODULES_PATTERN)) + const index = dep.path.indexOf(NODE_MODULES_PATTERN) + if (index < 0) { + throw new Error("cannot find node_modules in the path " + dep.path) + } + + const root = dep.path.substring(0, index) let list = rootPathToCopier.get(root) if (list == null) { list = [] @@ -111,7 +123,7 @@ async function copyHoistedNodeModules(packager: Packager, mainMatcher: FileMatch const matcher = new FileMatcher(source, mainMatcher.to, mainMatcher.macroExpander, mainMatcher.patterns) const copier = new NodeModuleCopyHelper(matcher, packager) const files = await copier.collectNodeModules(rootPathToCopier.get(source)!!) - return {src: matcher.from, destination: matcher.to, files, metadata: copier.metadata} + return validateFileSet({src: matcher.from, destination: matcher.to, files, metadata: copier.metadata}) }) } diff --git a/packages/electron-builder-lib/src/util/packageDependencies.ts b/packages/electron-builder-lib/src/util/packageDependencies.ts index c3b33a70426..329df44856c 100644 --- a/packages/electron-builder-lib/src/util/packageDependencies.ts +++ b/packages/electron-builder-lib/src/util/packageDependencies.ts @@ -42,9 +42,9 @@ export function createLazyProductionDeps(projectDir: string) { /** @internal */ export async function getProductionDependencies(folder: string): Promise> { - const sorted: Array = [] - computeSortedPaths(await new Collector().collect(folder), sorted, false) - return sorted + const result: Array = [] + computeSortedPaths(await new Collector().collect(folder), result, false) + return result } const ignoredProperties = new Set(["description", "author", "bugs", "engines", "repository", "build", "main", "license", "homepage", "scripts", "maintainers", "contributors", "keywords", "devDependencies", "files", "typings", "types", "xo", "resolutions"]) @@ -78,13 +78,13 @@ class Collector { this.unmarkExtraneous(rootDependency) if (this.unresolved.size > 0) { - log.debug({unresolved: Array.from(this.unresolved.keys()).join(", ")}, "Unresolved dependencies after first round") + log.debug({unresolved: Array.from(this.unresolved.keys()).join(", ")}, "unresolved dependencies after first round") await this.resolveUnresolvedHoisted(rootDependency, dir) } return rootDependency } - private async resolveUnresolvedHoisted(rootDependency: Dependency, dir: string) { + private async resolveUnresolvedHoisted(rootDependency: Dependency, dir: string): Promise { let nameToMetadata = rootDependency.dependencies if (nameToMetadata == null) { rootDependency.dependencies = new Map() @@ -112,30 +112,50 @@ class Collector { const parentNodeModulesDir = parentDir + path.sep + "node_modules" const dirStat = await statOrNull(parentNodeModulesDir) if (dirStat == null || !dirStat.isDirectory()) { - continue + if (dirStat == null || !dirStat.isDirectory()) { + continue + } } - const unresolved = Array.from(this.unresolved.keys()) - this.unresolved.clear() - - const resolved = await BluebirdPromise.map(unresolved, it => { - return this.readChildPackage(it, parentNodeModulesDir, rootDependency) - .catch(e => { - if ((e as any).code === "ENOENT") { - return null - } - else { - throw e - } - }) - }, CONCURRENCY) - for (const dep of resolved) { - if (dep != null) { - nameToMetadata.set(dep.realName, dep) + // https://github.com/electron-userland/electron-builder/issues/2222#issuecomment-339060335 + // step 1: resolve current unresolved + // step n: try to resolve new unresolved in the same parent dir until at least something is resolved in the dir + while (true) { + const unresolved = Array.from(this.unresolved.keys()) + this.unresolved.clear() + + const resolved = await BluebirdPromise.map(unresolved, it => { + return this.readChildPackage(it, parentNodeModulesDir, rootDependency) + .catch(e => { + if ((e as any).code === "ENOENT") { + return null + } + else { + throw e + } + }) + }, CONCURRENCY) + + let hasResolved = false + + for (const dep of resolved) { + if (dep != null) { + hasResolved = true + this.unmarkExtraneous(dep) + nameToMetadata.set(dep.realName, dep) + } } - } - this.unmarkExtraneous(rootDependency) + if (!hasResolved) { + break + } + + this.unmarkExtraneous(rootDependency) + + if (this.unresolved.size === 0) { + return + } + } } while (this.unresolved.size > 0) } diff --git a/yarn.lock b/yarn.lock index a1356b42a24..8ac18fb6bac 100644 --- a/yarn.lock +++ b/yarn.lock @@ -794,9 +794,9 @@ babel-preset-jest@^23.0.0-alpha.0: babel-plugin-jest-hoist "^23.0.0-alpha.0" babel-plugin-syntax-object-rest-spread "^6.13.0" -babel-preset-ts-node6-bluebird@^2.0.1: - version "2.0.1" - resolved "https://registry.yarnpkg.com/babel-preset-ts-node6-bluebird/-/babel-preset-ts-node6-bluebird-2.0.1.tgz#846f137112e0f8c0fca76dda696d8bd64e9be25e" +babel-preset-ts-node6-bluebird@^2.0.3: + version "2.0.3" + resolved "https://registry.yarnpkg.com/babel-preset-ts-node6-bluebird/-/babel-preset-ts-node6-bluebird-2.0.3.tgz#d246288f9261f93ec0812e757aa217554ff020ae" dependencies: "@babel/helper-plugin-utils" "^7.0.0-beta.42" "@babel/plugin-transform-modules-commonjs" "^7.0.0-beta.42" @@ -5615,9 +5615,9 @@ truncate-utf8-bytes@^1.0.0: dependencies: utf8-byte-length "^1.0.1" -ts-babel@^5.0.1: - version "5.0.1" - resolved "https://registry.yarnpkg.com/ts-babel/-/ts-babel-5.0.1.tgz#0b096306ff3180704441bdeecd61b51b37b8c434" +ts-babel@^5.0.2: + version "5.0.2" + resolved "https://registry.yarnpkg.com/ts-babel/-/ts-babel-5.0.2.tgz#fefeb9a26ca5e6195fde67961665a4e27d9cada0" dependencies: "@babel/core" "^7.0.0-beta.42" bluebird-lst "^1.0.5"