Skip to content

Commit

Permalink
feat: allow deep import of any package
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Wiles committed Feb 3, 2020
1 parent 515778b commit a7d8653
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 17 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
bazel-*
.idea
.idea
yarn-error.log
.vscode
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
"@bazel/jasmine": "1.1.0",
"@bazel/typescript": "1.1.0",
"@types/jasmine": "3.5.0",
"@types/shelljs": "0.8.6",
"@types/mock-fs": "4.10.0",
"@bazel/bazel": "2.0.0",
"@types/node": "12.7.8",
"mock-fs": "4.10.4"
},
Expand Down
1 change: 1 addition & 0 deletions src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ts_library(
],
deps = [
"@npm//@types/node",
"@npm//@types/shelljs",
] + RUNTIME_NPM_DEPS,
module_name = "@evertz/bzlgen/src",
)
Expand Down
31 changes: 20 additions & 11 deletions src/generators/ts/ts.generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parse } from 'path';
import { parse, sep, join } from 'path';

import { tsquery } from '@phenomnomnominal/tsquery';
import { ExportDeclaration, Expression, ImportDeclaration, SourceFile } from 'typescript';
Expand Down Expand Up @@ -106,20 +106,29 @@ export class TsGenerator extends BuildFileGenerator {
let label = this.workspace.tryResolveLabelFromStaticMapping(imp, undefined, '.');
if (label) { return label; }

// some imports are deep but resolve to one label (eg material and cdk)
if (imp.startsWith('@angular/material')) {
return Label.parseAbsolute(`@${npmWorkspace}//@angular/material`);
} else if (imp.startsWith('@angular/cdk')) {
return Label.parseAbsolute(`@${npmWorkspace}//@angular/material`);
}
const relative = this.workspace.isWorkspaceRelative(imp) || imp.startsWith('.');

if (imp.startsWith('.')) {
if (relative) {
label = this.workspace.getLabelForFile(imp + '.ts');
if (label) { return label; }
}

// fall back to assuming 3rd_party
return Label.parseAbsolute(`@${npmWorkspace}//${imp}`);
throw new Error (`Unable to generate label for: ${imp}`)
} else {
// module specifiers do not use system seperators
// they always use forward slashes
const pathParts = imp.split('/');
// remove any deep imports by stripping any paths past the end of the package name
let reducedPathed: string;
if(imp.startsWith('@')) {
// a scoped npm package cannot have more than two segments
reducedPathed = join(...pathParts.slice(0, 2));
} else {
// a normal npm package cannot have more than one segment
reducedPathed = join(...pathParts.slice(0, 1));

}
return Label.parseAbsolute(`@${npmWorkspace}//${reducedPathed}`);
}
}

}
1 change: 1 addition & 0 deletions test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ts_library(
"@npm//@types/node",
"@npm//jasmine",
"@npm//mock-fs",
"@npm//@types/mock-fs",
],
)

Expand Down
30 changes: 25 additions & 5 deletions test/generators/ts.generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export class Some {}

workspace = new Workspace(setupAndParseArgs(argv, true, 0));
gen = new TsGenerator(workspace);
})

afterEach(() => mockfs.restore());
it('can generate ts_library with deps', async () => {
mockfs({
'/home/workspace/src/some': {
'one.ts': TS_ONE,
Expand All @@ -49,12 +52,8 @@ export class Some {}
'foo.ts': '',
}
});
});

afterEach(() => mockfs.restore());

it('can generate ts_library with deps', () => {
gen.generate();
await gen.generate();

const commands = workspace.getBuildozer().toCommands();

Expand All @@ -67,4 +66,25 @@ export class Some {}

expect(commands.join('\n')).toEqual(expected);
});

it('can strip all deep imports', async () => {
mockfs({
'/home/workspace/src/some': {
'one.ts': `import {} from 'package'; import {} from 'package/deep'; import {} from '@scope/package'; import {} from '@scope/package/deep';`
},
},);

await gen.generate();

const commands = workspace.getBuildozer().toCommands();

const expected =`new_load @npm_bazel_typescript//:index.bzl ts_library|//src/some:__pkg__
new ts_library some|//src/some:__pkg__
add srcs one.ts|//src/some:some
add deps @npm//package:package @npm//@scope/package:package|//src/some:some
set tsconfig "//:tsconfig"|//src/some:some`

expect(commands.join('\n')).toEqual(expected);
});

});
70 changes: 70 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,42 @@
# yarn lockfile v1


"@bazel/[email protected]":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@bazel/bazel-darwin_x64/-/bazel-darwin_x64-2.0.0.tgz#bd678069216dd470c6816a22c405f21e7f048038"
integrity sha512-I/pP+B+2xfY0g+OEpEcVnk8rizuC761pAzBOQjP3b+gz3AzeRgm05CpcSY7tfPIppMSYoy3uTZJ1XlwgUg7IQQ==

"@bazel/[email protected]":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@bazel/bazel-linux_x64/-/bazel-linux_x64-2.0.0.tgz#2c76e3301e9178a90ec3ad00649e89b953eda0b7"
integrity sha512-iOr45G+511IbP7e+ISriG97WpfCAVXekTrTgL5mGg3NDBFCVNs350VquHAvmlXAoP5+IEug2pCOlkdEl4bLl8g==

"@bazel/[email protected]":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@bazel/bazel-win32_x64/-/bazel-win32_x64-2.0.0.tgz#f12ac0738d2eac0fd255f099776194807cedfe50"
integrity sha512-5qs2qoa/paG/YYEM0yvrwuJIShoPVK2FX+Oz9jEWAQJsmU4drHA9Aq+gbBOirEFLmvYhleZ9XORCwu/5uAo8vA==

"@bazel/bazel@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@bazel/bazel/-/bazel-2.0.0.tgz#feb8cf5a40ea6437ef69cac2a92072118b11c230"
integrity sha512-KQbv5dHNSfutbhXCc3KVMuBXPpUh6Af/hT9IRIaMTuiB6Nq2gEW9Z3aNqncopdZqV848V/qYxnqPnQ+S37fMyQ==
dependencies:
"@bazel/hide-bazel-files" latest
optionalDependencies:
"@bazel/bazel-darwin_x64" "2.0.0"
"@bazel/bazel-linux_x64" "2.0.0"
"@bazel/bazel-win32_x64" "2.0.0"

"@bazel/[email protected]":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@bazel/hide-bazel-files/-/hide-bazel-files-1.1.0.tgz#a7d46a4aa3717150b54fde05b2f8b6bab733f493"
integrity sha512-JJ9eMKPBodWugC4Wu0QLe0saDZ971RcP2kjZbG1vJfQHn/gnxvw+4nXDRwei5EzxBsGGuLxY1MkNDtRFIfZr5w==

"@bazel/hide-bazel-files@latest":
version "1.2.4"
resolved "https://registry.yarnpkg.com/@bazel/hide-bazel-files/-/hide-bazel-files-1.2.4.tgz#fe00f2bbceb8e1a17bedde7616bef4810849c7f5"
integrity sha512-KZX4MkJOMtZT1DwFmjCWJNCUKcwbYBrv3Q++peoWkzJPcHGbt5nLFjt8Gzagi2GY7GRqp8WAut0E+eOGdhvkEg==

"@bazel/[email protected]":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@bazel/jasmine/-/jasmine-1.1.0.tgz#3900a99b57ceac1e979aa0d64b624a70a4ebd92e"
Expand Down Expand Up @@ -86,6 +117,20 @@
resolved "https://registry.yarnpkg.com/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570"
integrity sha1-p3c2C1s5oaLlEG+OhY8v0tBgxXA=

"@types/events@*":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@types/events/-/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7"
integrity sha512-EaObqwIvayI5a8dCzhFrjKzVwKLxjoG9T6Ppd5CEo07LRKfQ8Yokw54r5+Wq7FaBQ+yXRvQAYPrHwya1/UFt9g==

"@types/glob@*":
version "7.1.1"
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.1.tgz#aa59a1c6e3fbc421e07ccd31a944c30eba521575"
integrity sha512-1Bh06cbWJUHMC97acuD6UMG29nMt0Aqz1vF3guLfG+kHHJhy3AyohZFFxYk2f7Q1SQIrNwvncxAE0N/9s70F2w==
dependencies:
"@types/events" "*"
"@types/minimatch" "*"
"@types/node" "*"

"@types/[email protected]":
version "3.5.0"
resolved "https://registry.yarnpkg.com/@types/jasmine/-/jasmine-3.5.0.tgz#2ad2006c8a937d20df20a8fee86071d0f730ef99"
Expand All @@ -96,6 +141,23 @@
resolved "https://registry.yarnpkg.com/@types/long/-/long-4.0.0.tgz#719551d2352d301ac8b81db732acb6bdc28dbdef"
integrity sha512-1w52Nyx4Gq47uuu0EVcsHBxZFJgurQ+rTKS3qMHxR1GY2T8c2AJYd6vZoZ9q1rupaDjU0yT+Jc2XTyXkjeMA+Q==

"@types/minimatch@*":
version "3.0.3"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"
integrity sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA==

"@types/mock-fs@^4.10.0":
version "4.10.0"
resolved "https://registry.yarnpkg.com/@types/mock-fs/-/mock-fs-4.10.0.tgz#460061b186993d76856f669d5317cda8a007c24b"
integrity sha512-FQ5alSzmHMmliqcL36JqIA4Yyn9jyJKvRSGV3mvPh108VFatX7naJDzSG4fnFQNZFq9dIx0Dzoe6ddflMB2Xkg==
dependencies:
"@types/node" "*"

"@types/node@*":
version "13.7.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-13.7.0.tgz#b417deda18cf8400f278733499ad5547ed1abec4"
integrity sha512-GnZbirvmqZUzMgkFn70c74OQpTTUcCzlhQliTzYjQMqg+hVKcDnxdL19Ne3UdYzdMA/+W3eb646FWn/ZaT1NfQ==

"@types/[email protected]":
version "12.7.8"
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.7.8.tgz#cb1bf6800238898bc2ff6ffa5702c3cadd350708"
Expand All @@ -106,6 +168,14 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.17.13.tgz#ccebcdb990bd6139cd16e84c39dc2fb1023ca90c"
integrity sha512-pMCcqU2zT4TjqYFrWtYHKal7Sl30Ims6ulZ4UFXxI4xbtQqK/qqKwkDoBFCfooRqqmRu9vY3xaJRwxSh673aYg==

"@types/shelljs@^0.8.6":
version "0.8.6"
resolved "https://registry.yarnpkg.com/@types/shelljs/-/shelljs-0.8.6.tgz#45193a51df99e0f00513c39a2152832399783221"
integrity sha512-svx2eQS268awlppL/P8wgDLBrsDXdKznABHJcuqXyWpSKJgE1s2clXlBvAwbO/lehTmG06NtEWJRkAk4tAgenA==
dependencies:
"@types/glob" "*"
"@types/node" "*"

ansi-regex@^2.0.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-2.1.1.tgz#c3b33ab5ee360d86e0e628f0468ae7ef27d654df"
Expand Down

0 comments on commit a7d8653

Please sign in to comment.