From cd3b380a1689e4f680b9daafd5685edd675a1a2f Mon Sep 17 00:00:00 2001 From: mitchellhamilton Date: Tue, 30 Jun 2020 15:07:16 +1000 Subject: [PATCH] Support bitbucket when gettings urls to markings --- .changeset/short-apes-chew.md | 5 ++++ packages/cli/package.json | 2 ++ packages/cli/src/index.ts | 45 +++++++++++++++++++++-------------- pnpm-lock.yaml | 14 +++++++++++ 4 files changed, 48 insertions(+), 18 deletions(-) create mode 100644 .changeset/short-apes-chew.md diff --git a/.changeset/short-apes-chew.md b/.changeset/short-apes-chew.md new file mode 100644 index 0000000..9f0e5f3 --- /dev/null +++ b/.changeset/short-apes-chew.md @@ -0,0 +1,5 @@ +--- +"@markings/cli": patch +--- + +Support Bitbucket when getting urls to markings diff --git a/packages/cli/package.json b/packages/cli/package.json index 22f3d4b..c6da90d 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -16,6 +16,7 @@ "@markings/types": "^0.0.7", "@types/babel__core": "^7.1.6", "@types/babel__traverse": "^7.0.9", + "@types/normalize-path": "^3.0.0", "@types/parse-github-url": "^1.0.0", "chalk": "^2.4.2", "fs-extra": "^8.1.0", @@ -23,6 +24,7 @@ "meow": "^5.0.0", "normalize-path": "^3.0.0", "p-limit": "^2.2.1", + "parse-bitbucket-url": "^0.3.0", "parse-github-url": "^1.0.2" }, "devDependencies": { diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index f93f01d..ed7d31e 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -1,4 +1,3 @@ -// @flow import * as logger from "./logger"; import fs from "fs-extra"; import nodePath from "path"; @@ -8,7 +7,7 @@ import { Marking, Source, Output, - PartialMarking + PartialMarking, } from "@markings/types"; import mod from "module"; import globby from "globby"; @@ -17,7 +16,10 @@ import { transform, PluginObj } from "@babel/core"; import { getPackages, Package } from "@manypkg/get-packages"; // @ts-ignore import { visitors as visitorsUtils, Visitor } from "@babel/traverse"; +// @ts-ignore +import parseBitbucketUrl from "parse-bitbucket-url"; import parseGithubUrl from "parse-github-url"; +import normalizePath from "normalize-path"; let parserPlugins: ParserPlugin[] = [ "asyncGenerators", @@ -32,7 +34,7 @@ let parserPlugins: ParserPlugin[] = [ "throwExpressions", "nullishCoalescingOperator", "optionalChaining", - "decorators-legacy" + "decorators-legacy", ]; function getPackageFromFilename( @@ -88,11 +90,11 @@ function getPackageFromFilename( }; await Promise.all( - config.sources.map(async sourceConfig => { + config.sources.map(async (sourceConfig) => { let result = await globby(sourceConfig.include, { cwd, absolute: true, - ignore: ["**/node_modules/**/*"] + ignore: ["**/node_modules/**/*"], }); for (let filename of result) { @@ -112,26 +114,33 @@ function getPackageFromFilename( getUrl = (filename, line) => { return `https://github.com/${parsed.owner}/${parsed.name}/blob/master/${filename}#L${line}`; }; + } else { + let bitbucketParsed = parseBitbucketUrl( + (pkgs.root.packageJson as any).repository + ); + getUrl = (filename, line) => { + return `https://bitbucket.org/${bitbucketParsed.owner}/${bitbucketParsed.name}/src/${bitbucketParsed.branch}/${filename}#lines-${line}`; + }; } } - let packagesByDirectory = new Map(pkgs.packages.map(x => [x.dir, x])); + let packagesByDirectory = new Map(pkgs.packages.map((x) => [x.dir, x])); // TODO: do extraction work in worker threads await Promise.all( [...sourcesByFilename.entries()].map(async ([filename, sources]) => { - let visitorsArray = [...sources].map(x => req(x).source.visitor); + let visitorsArray = [...sources].map((x) => req(x).source.visitor); let visitor: Visitor = visitorsUtils.merge( visitorsArray, - [...sources].map(source => ({ + [...sources].map((source) => ({ addMarking: (marking: PartialMarking) => { markings.push({ location: { line: marking.location.line, filename, link: getUrl( - nodePath.relative(pkgs.root.dir, filename), + normalizePath(nodePath.relative(pkgs.root.dir, filename)), marking.location.line - ) + ), }, description: marking.description, source: source, @@ -140,9 +149,9 @@ function getPackageFromFilename( filename, packagesByDirectory ).packageJson.name, - purpose: marking.purpose + purpose: marking.purpose, }); - } + }, })) ); let contents = await fs.readFile(filename, "utf8"); @@ -156,26 +165,26 @@ function getPackageFromFilename( parserOpts: { plugins: parserPlugins.concat( /\.tsx?$/.test(filename) ? "typescript" : "flow" - ) + ), }, plugins: [ (): PluginObj => { return { - visitor + visitor, }; - } - ] + }, + ], }); }) ); await Promise.all( - config.outputs.map(async outputConfig => { + config.outputs.map(async (outputConfig) => { let plugin: Output = req(outputConfig.output).output; let output = await plugin.getFile(markings); await fs.writeFile(outputConfig.filename, output); }) ); -})().catch(err => { +})().catch((err) => { console.log("yes"); if (err instanceof ExitError) { process.exit(err.code); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 958f716..0265951 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -40,6 +40,7 @@ importers: '@markings/types': 'link:../types' '@types/babel__core': 7.1.6 '@types/babel__traverse': 7.0.9 + '@types/normalize-path': 3.0.0 '@types/parse-github-url': 1.0.0 chalk: 2.4.2 fs-extra: 8.1.0 @@ -47,6 +48,7 @@ importers: meow: 5.0.0 normalize-path: 3.0.0 p-limit: 2.2.2 + parse-bitbucket-url: 0.3.0 parse-github-url: 1.0.2 devDependencies: fixturez: 1.1.0 @@ -60,6 +62,7 @@ importers: '@markings/types': ^0.0.7 '@types/babel__core': ^7.1.6 '@types/babel__traverse': ^7.0.9 + '@types/normalize-path': ^3.0.0 '@types/parse-github-url': ^1.0.0 chalk: ^2.4.2 fixturez: ^1.1.0 @@ -68,6 +71,7 @@ importers: meow: ^5.0.0 normalize-path: ^3.0.0 p-limit: ^2.2.1 + parse-bitbucket-url: ^0.3.0 parse-github-url: ^1.0.2 strip-ansi: ^6.0.0 packages/output-csv: @@ -2996,6 +3000,10 @@ packages: dev: false resolution: integrity: sha512-E6M6N0blf/jiZx8Q3nb0vNaswQeEyn0XlupO+xN6DtJ6r6IT4nXrTry7zhIfYvFCl3/8Cu6WIysmUBKiqV0bqQ== + /@types/normalize-path/3.0.0: + dev: false + resolution: + integrity: sha512-Nd8y/5t/7CRakPYiyPzr/IAfYusy1FkcZYFEAcoMZkwpJv2n4Wm+olW+e7xBdHEXhOnWdG9ddbar0gqZWS4x5Q== /@types/parse-github-url/1.0.0: dependencies: '@types/node': 13.9.1 @@ -8328,6 +8336,12 @@ packages: dev: false resolution: integrity: sha512-jkMYn1dcJqF6d5CpU689bq7w/b5ALS9ROVSpQDPrZsqqesUJii9qutvoT5ltGedNXMO2e16YUWIghG9KxaViTQ== + /parse-bitbucket-url/0.3.0: + dev: false + engines: + node: '>=0.10.0' + resolution: + integrity: sha1-+s7Dvuv87EImVWXkkIGhB1m7uhc= /parse-github-url/1.0.2: dev: false engines: