Comparing version 1.3.0 to 1.4.0
#!/usr/bin/env node | ||
require('..').cli(process.argv.slice(2)); | ||
import process from 'process'; | ||
import { cli } from '../lib/cli.js'; | ||
cli(process.argv.slice(2)); |
@@ -1,11 +0,10 @@ | ||
const clipboardy = require('clipboardy'); | ||
const matter = require('gray-matter'); | ||
const { | ||
import clipboardy from 'clipboardy'; | ||
import matter from 'gray-matter'; | ||
import { | ||
containsOnlyAlphanumeric, | ||
removeLocaleFromUrl, | ||
splitHashFromUrl, | ||
addLocaleToUrl | ||
} = require('./util'); | ||
} from './util.js'; | ||
const trackedDomains = [ | ||
export const trackedDomains = [ | ||
'azure.com', | ||
@@ -17,20 +16,23 @@ 'azure.microsoft.com', | ||
'cloudblogs.microsoft.com', | ||
'devblogs.microsoft.com ', | ||
'devblogs.microsoft.com', | ||
'docs.microsoft.com', | ||
'marketplace.visualstudio.com', | ||
'techcommunity.microsoft.com' | ||
'techcommunity.microsoft.com', | ||
'microsoftazurepass.com', | ||
'account.microsoft.com' | ||
]; | ||
const trackingParametersRegex = /wt\.mc_id=\w+-\w+-\w+/gi; | ||
const trackingParam = 'WT.mc_id'; | ||
const urlRegex = /\bhttps?:\/\/[-\w+&@#/%?=~|!:,.;]*[-\w+&@#/%=~|]/gim; | ||
function isTrackedDomain(url) { | ||
const {hostname} = new URL(url); | ||
export function isTrackedDomain(url) { | ||
const { hostname } = new URL(url); | ||
return trackedDomains.some((domain) => hostname.includes(domain)); | ||
} | ||
function checkTrackedDomain(url) { | ||
export function checkTrackedDomain(url) { | ||
if (!isTrackedDomain(url)) throw new Error('URL domain is not tracked'); | ||
} | ||
function mergeTrackingCode(baseTrackingCode, otherTrackingCode) { | ||
export function mergeTrackingCode(baseTrackingCode, otherTrackingCode) { | ||
const base = baseTrackingCode ? parseTrackingCode(baseTrackingCode) : {}; | ||
@@ -48,4 +50,4 @@ const other = otherTrackingCode ? parseTrackingCode(otherTrackingCode) : {}; | ||
function mergeFrontMatterTrackingCode(trackingCode, text) { | ||
const {data} = matter(text); | ||
export function mergeFrontMatterTrackingCode(trackingCode, text) { | ||
const { data } = matter(text); | ||
const code = data.trackingCode || data.tracking_code || data['tracking-code']; | ||
@@ -59,5 +61,10 @@ if (code) { | ||
const generateTrackingParameters = (trackingCode) => `WT.mc_id=${trackingCode}`; | ||
export function generateTrackingParameters(trackingCode, params = {}) { | ||
return new URLSearchParams({ | ||
...params, | ||
[trackingParam]: trackingCode | ||
}).toString(); | ||
} | ||
function parseTrackingCode(partialTrackingCode) { | ||
export function parseTrackingCode(partialTrackingCode) { | ||
if (!partialTrackingCode) throw new Error('no tracking code defined'); | ||
@@ -80,6 +87,6 @@ | ||
return {area, id, alias}; | ||
return { area, id, alias }; | ||
} | ||
function updateTrackedUrl(url, trackingCode, locale) { | ||
export function updateTrackedUrl(url, trackingCode, locale, extraParams = {}) { | ||
if (locale !== true) url = removeLocaleFromUrl(url); | ||
@@ -89,17 +96,19 @@ | ||
const newTrackingParameters = generateTrackingParameters(trackingCode); | ||
if (trackingParametersRegex.test(url)) { | ||
// Update tracking params | ||
return url.replace(trackingParametersRegex, newTrackingParameters); | ||
} | ||
// Add tracking params | ||
const separator = url.indexOf('?') > 0 ? '&' : '?'; | ||
const [baseUrl, hash] = splitHashFromUrl(url); | ||
return baseUrl + separator + newTrackingParameters + hash; | ||
const currentUrl = new URL(url); | ||
const existingParams = Object.fromEntries(currentUrl.searchParams.entries()); | ||
const newTrackingParameters = generateTrackingParameters(trackingCode, { | ||
...existingParams, | ||
...extraParams | ||
}); | ||
currentUrl.search = newTrackingParameters; | ||
return currentUrl.href; | ||
} | ||
function updateTrackedUrlAndCopy(url, trackingCode, locale) { | ||
const newUrl = updateTrackedUrl(url, trackingCode, locale); | ||
export function updateTrackedUrlAndCopy( | ||
url, | ||
trackingCode, | ||
locale, | ||
extraParams | ||
) { | ||
const newUrl = updateTrackedUrl(url, trackingCode, locale, extraParams); | ||
clipboardy.writeSync(newUrl); | ||
@@ -109,21 +118,15 @@ return newUrl; | ||
function updateTrackingCodeInText(text, trackingCode, locale) { | ||
export function updateTrackingCodeInText( | ||
text, | ||
trackingCode, | ||
locale, | ||
extraParams | ||
) { | ||
return text | ||
? text.replace(urlRegex, (url) => | ||
isTrackedDomain(url) ? updateTrackedUrl(url, trackingCode, locale) : url | ||
isTrackedDomain(url) | ||
? updateTrackedUrl(url, trackingCode, locale, extraParams) | ||
: url | ||
) | ||
: text; | ||
} | ||
module.exports = { | ||
trackedDomains, | ||
isTrackedDomain, | ||
checkTrackedDomain, | ||
mergeTrackingCode, | ||
mergeFrontMatterTrackingCode, | ||
generateTrackingParameters, | ||
parseTrackingCode, | ||
updateTrackedUrl, | ||
updateTrackedUrlAndCopy, | ||
updateTrackingCodeInText | ||
}; |
@@ -1,41 +0,28 @@ | ||
const chalk = require('chalk'); | ||
import process from 'process'; | ||
import chalk from 'chalk'; | ||
const containsOnlyAlphanumeric = (string) => /^\w+$/.test(string); | ||
export const containsOnlyAlphanumeric = (string) => /^\w+$/.test(string); | ||
const isUrl = (string) => /^https?:\/\/\w/.test(string); | ||
export const isUrl = (string) => /^https?:\/\/\w/.test(string); | ||
const removeLocaleFromUrl = (url) => | ||
export const removeLocaleFromUrl = (url) => | ||
url.replace(/microsoft.com\/\w{2}-\w{2}\//g, 'microsoft.com/'); | ||
const addLocaleToUrl = (url, locale) => | ||
export const addLocaleToUrl = (url, locale) => | ||
locale ? url.replace(/microsoft.com\//g, `microsoft.com/${locale}/`) : url; | ||
function checkLocale(locale) { | ||
export const parseQueryParams = (queryString) => | ||
Object.fromEntries(new URLSearchParams(queryString).entries()); | ||
export function checkLocale(locale) { | ||
if (!/^\w{2}-\w{2}$/.test(locale)) throw new Error('Invalid locale format'); | ||
} | ||
function splitHashFromUrl(url) { | ||
const hashIndex = url.indexOf('#'); | ||
return hashIndex === -1 | ||
? [url, ''] | ||
: [url.slice(0, hashIndex), url.slice(hashIndex)]; | ||
} | ||
function safeRun(func) { | ||
export function safeRun(func) { | ||
try { | ||
return func() || true; | ||
} catch (error) { | ||
console.error(chalk`{yellow Error: ${error.message}}`); | ||
console.error(chalk.yellow`Error: ${error.message}`); | ||
process.exitCode = -1; | ||
} | ||
} | ||
module.exports = { | ||
containsOnlyAlphanumeric, | ||
isUrl, | ||
removeLocaleFromUrl, | ||
addLocaleToUrl, | ||
checkLocale, | ||
splitHashFromUrl, | ||
safeRun | ||
}; |
{ | ||
"name": "cxa-track", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Convenient CLI to quickly update CxA tracked links", | ||
"preferGlobal": true, | ||
"repository": "sinedied/cxa-track", | ||
"homepage": "https://github.com/sinedied/cxa-track", | ||
"bugs": { | ||
"url": "https://github.com/sinedied/cxa-track/issues" | ||
"type": "module", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/sinedied/cxa-track" | ||
}, | ||
"keywords": [ | ||
"cli", | ||
"cxa", | ||
"advocate", | ||
"microsoft", | ||
"links" | ||
], | ||
"main": "cli.js", | ||
"exports": "./lib/index.js", | ||
"bin": { | ||
@@ -23,8 +15,9 @@ "cxa": "./bin/cxa.js" | ||
"scripts": { | ||
"commit": "git-cz", | ||
"lint": "xo", | ||
"lint:fix": "xo --fix", | ||
"test": "xo && jest --verbose", | ||
"test:watch": "jest --watch", | ||
"release:check": "semantic-release --dry-run" | ||
"test": "xo && npm run -s test:unit", | ||
"test:unit": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --verbose", | ||
"test:watch": "npm run -s test:unit -- --watch", | ||
"release:check": "semantic-release --dry-run", | ||
"prepare": "husky install" | ||
}, | ||
@@ -35,26 +28,38 @@ "author": { | ||
}, | ||
"homepage": "https://github.com/sinedied/cxa-track", | ||
"bugs": { | ||
"url": "https://github.com/sinedied/cxa-track/issues" | ||
}, | ||
"keywords": [ | ||
"cli", | ||
"cxa", | ||
"advocate", | ||
"microsoft", | ||
"links" | ||
], | ||
"license": "MIT", | ||
"dependencies": { | ||
"chalk": "^4.1.0", | ||
"clipboardy": "^2.3.0", | ||
"conf": "^7.1.2", | ||
"chalk": "^5.0.1", | ||
"clipboardy": "^3.0.0", | ||
"conf": "^10.2.0", | ||
"gray-matter": "^4.0.2", | ||
"minimist": "^1.2.5", | ||
"update-notifier": "^5.0.1" | ||
"update-notifier": "^6.0.2" | ||
}, | ||
"devDependencies": { | ||
"@commitlint/cli": "^11.0.0", | ||
"@commitlint/config-conventional": "^11.0.0", | ||
"commitizen": "^4.2.2", | ||
"cz-conventional-changelog": "^3.3.0", | ||
"husky": "^4.3.7", | ||
"jest": "^26.6.3", | ||
"lint-staged": "^10.5.3", | ||
"semantic-release": "^17.3.1", | ||
"semantic-release-npm-github": "^1.0.2", | ||
"xo": "^0.37.1" | ||
"cross-env": "^7.0.3", | ||
"husky": "^8.0.1", | ||
"jest": "^28.1.3", | ||
"lint-staged": "^13.0.3", | ||
"semantic-release": "^19.0.3", | ||
"semantic-release-npm-github": "^3.0.0", | ||
"xo": "^0.51.0" | ||
}, | ||
"engines": { | ||
"node": ">=12.0.0" | ||
"node": ">=14.0.0" | ||
}, | ||
"prettier": { | ||
"trailingComma": "none", | ||
"bracketSpacing": true | ||
}, | ||
"xo": { | ||
@@ -66,3 +71,7 @@ "space": true, | ||
"jest" | ||
] | ||
], | ||
"rules": { | ||
"unicorn/prevent-abbreviations": "off", | ||
"unicorn/prefer-node-protocol": "off" | ||
} | ||
}, | ||
@@ -77,9 +86,5 @@ "jest": { | ||
], | ||
"resolver": "<rootDir>/test/jest.resolver.cjs", | ||
"silent": true | ||
}, | ||
"commitlint": { | ||
"extends": [ | ||
"@commitlint/config-conventional" | ||
] | ||
}, | ||
"lint-staged": { | ||
@@ -95,13 +100,2 @@ "*.js": [ | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"precommit": "lint-staged", | ||
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS" | ||
} | ||
}, | ||
"config": { | ||
"commitizen": { | ||
"path": "cz-conventional-changelog" | ||
} | ||
}, | ||
"files": [ | ||
@@ -108,0 +102,0 @@ "index.js", |
@@ -27,3 +27,3 @@ # :memo: cxa-track | ||
```sh | ||
``` | ||
Usage: cxa [options] [<files|URL>] | ||
@@ -39,2 +39,3 @@ | ||
-l, --locale <locale-code> Force locale code in URLs | ||
-e, --extra Extra query params to update | ||
-h, --help Show this help | ||
@@ -48,2 +49,3 @@ | ||
Locale format: <language>-<country>, example: en-us | ||
Extra format: <param1>=<value1>&<param2>=<value2>, example: foo=bar&baz=qux | ||
``` | ||
@@ -50,0 +52,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unpublished package
Supply chain riskPackage version was not found on the registry. It may exist on a different registry and need to be configured to pull from that registry.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unpopular package
QualityThis package is not very popular.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
7
343
0
69
1
Yes
17188
7
1
+ Added@pnpm/config.env-replace@1.1.0(transitive)
+ Added@pnpm/network.ca-file@1.0.2(transitive)
+ Added@pnpm/npm-conf@2.3.1(transitive)
+ Added@sindresorhus/is@5.6.0(transitive)
+ Added@szmarczak/http-timer@5.0.1(transitive)
+ Added@types/http-cache-semantics@4.0.4(transitive)
+ Addedajv@8.17.1(transitive)
+ Addedajv-formats@2.1.1(transitive)
+ Addedansi-regex@6.1.0(transitive)
+ Addedansi-styles@6.2.1(transitive)
+ Addedboxen@7.1.1(transitive)
+ Addedcacheable-lookup@7.0.0(transitive)
+ Addedcacheable-request@10.2.14(transitive)
+ Addedcamelcase@7.0.1(transitive)
+ Addedchalk@5.3.0(transitive)
+ Addedci-info@3.9.0(transitive)
+ Addedcli-boxes@3.0.0(transitive)
+ Addedclipboardy@3.0.0(transitive)
+ Addedconf@10.2.0(transitive)
+ Addedconfig-chain@1.1.13(transitive)
+ Addedconfigstore@6.0.0(transitive)
+ Addedcross-spawn@7.0.6(transitive)
+ Addedcrypto-random-string@4.0.0(transitive)
+ Addeddecompress-response@6.0.0(transitive)
+ Addeddefer-to-connect@2.0.1(transitive)
+ Addeddot-prop@6.0.1(transitive)
+ Addedeastasianwidth@0.2.0(transitive)
+ Addedemoji-regex@9.2.2(transitive)
+ Addedescape-goat@4.0.0(transitive)
+ Addedexeca@5.1.1(transitive)
+ Addedfast-uri@3.0.3(transitive)
+ Addedform-data-encoder@2.1.4(transitive)
+ Addedget-stream@6.0.1(transitive)
+ Addedgot@12.6.1(transitive)
+ Addedgraceful-fs@4.2.10(transitive)
+ Addedhas-yarn@3.0.0(transitive)
+ Addedhttp2-wrapper@2.2.1(transitive)
+ Addedhuman-signals@2.1.0(transitive)
+ Addedimport-lazy@4.0.0(transitive)
+ Addedis-ci@3.0.1(transitive)
+ Addedis-npm@6.0.0(transitive)
+ Addedis-stream@2.0.1(transitive)
+ Addedis-yarn-global@0.4.1(transitive)
+ Addedjson-buffer@3.0.1(transitive)
+ Addedjson-schema-traverse@1.0.0(transitive)
+ Addedkeyv@4.5.4(transitive)
+ Addedlatest-version@7.0.0(transitive)
+ Addedlowercase-keys@3.0.0(transitive)
+ Addedmerge-stream@2.0.0(transitive)
+ Addedmimic-response@3.1.04.0.0(transitive)
+ Addednormalize-url@8.0.1(transitive)
+ Addednpm-run-path@4.0.1(transitive)
+ Addedp-cancelable@3.0.0(transitive)
+ Addedpackage-json@8.1.1(transitive)
+ Addedpath-key@3.1.1(transitive)
+ Addedproto-list@1.2.4(transitive)
+ Addedpupa@3.1.0(transitive)
+ Addedquick-lru@5.1.1(transitive)
+ Addedregistry-auth-token@5.0.2(transitive)
+ Addedregistry-url@6.0.1(transitive)
+ Addedrequire-from-string@2.0.2(transitive)
+ Addedresolve-alpn@1.2.1(transitive)
+ Addedresponselike@3.0.0(transitive)
+ Addedsemver-diff@4.0.0(transitive)
+ Addedshebang-command@2.0.0(transitive)
+ Addedshebang-regex@3.0.0(transitive)
+ Addedstring-width@5.1.2(transitive)
+ Addedstrip-ansi@7.1.0(transitive)
+ Addedstrip-final-newline@2.0.0(transitive)
+ Addedtype-fest@1.4.02.19.0(transitive)
+ Addedunique-string@3.0.0(transitive)
+ Addedupdate-notifier@6.0.2(transitive)
+ Addedwhich@2.0.2(transitive)
+ Addedwidest-line@4.0.1(transitive)
+ Addedwrap-ansi@8.1.0(transitive)
+ Addedxdg-basedir@5.1.0(transitive)
- Removed@sindresorhus/is@0.14.0(transitive)
- Removed@szmarczak/http-timer@1.1.2(transitive)
- Removedajv@6.12.6(transitive)
- Removedansi-styles@4.3.0(transitive)
- Removedboxen@5.1.2(transitive)
- Removedcacheable-request@6.1.0(transitive)
- Removedcamelcase@6.3.0(transitive)
- Removedchalk@4.1.2(transitive)
- Removedci-info@2.0.0(transitive)
- Removedcli-boxes@2.2.1(transitive)
- Removedclipboardy@2.3.0(transitive)
- Removedclone-response@1.0.3(transitive)
- Removedcolor-convert@2.0.1(transitive)
- Removedcolor-name@1.1.4(transitive)
- Removedconf@7.1.2(transitive)
- Removedconfigstore@5.0.1(transitive)
- Removedcross-spawn@6.0.6(transitive)
- Removedcrypto-random-string@2.0.0(transitive)
- Removeddecompress-response@3.3.0(transitive)
- Removeddefer-to-connect@1.1.3(transitive)
- Removeddot-prop@5.3.0(transitive)
- Removedduplexer3@0.1.5(transitive)
- Removedend-of-stream@1.4.4(transitive)
- Removedescape-goat@2.1.1(transitive)
- Removedexeca@1.0.0(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedget-stream@4.1.05.2.0(transitive)
- Removedgot@9.6.0(transitive)
- Removedhas-flag@4.0.0(transitive)
- Removedhas-yarn@2.1.0(transitive)
- Removedimport-lazy@2.1.0(transitive)
- Removedis-ci@2.0.0(transitive)
- Removedis-npm@5.0.0(transitive)
- Removedis-stream@1.1.0(transitive)
- Removedis-yarn-global@0.3.0(transitive)
- Removedjson-buffer@3.0.0(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedkeyv@3.1.0(transitive)
- Removedlatest-version@5.1.0(transitive)
- Removedlowercase-keys@1.0.12.0.0(transitive)
- Removedmake-dir@3.1.0(transitive)
- Removedmimic-response@1.0.1(transitive)
- Removednice-try@1.0.5(transitive)
- Removednormalize-url@4.5.1(transitive)
- Removednpm-run-path@2.0.2(transitive)
- Removedonce@1.4.0(transitive)
- Removedp-cancelable@1.1.0(transitive)
- Removedp-finally@1.0.0(transitive)
- Removedpackage-json@6.5.0(transitive)
- Removedpath-key@2.0.1(transitive)
- Removedprepend-http@2.0.0(transitive)
- Removedpump@3.0.2(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedpupa@2.1.1(transitive)
- Removedregistry-auth-token@4.2.2(transitive)
- Removedregistry-url@5.1.0(transitive)
- Removedresponselike@1.0.2(transitive)
- Removedsemver@5.7.26.3.1(transitive)
- Removedsemver-diff@3.1.1(transitive)
- Removedshebang-command@1.2.0(transitive)
- Removedshebang-regex@1.0.0(transitive)
- Removedstrip-eof@1.0.0(transitive)
- Removedsupports-color@7.2.0(transitive)
- Removedto-readable-stream@1.0.0(transitive)
- Removedtype-fest@0.20.2(transitive)
- Removedunique-string@2.0.0(transitive)
- Removedupdate-notifier@5.1.0(transitive)
- Removeduri-js@4.4.1(transitive)
- Removedurl-parse-lax@3.0.0(transitive)
- Removedwhich@1.3.1(transitive)
- Removedwidest-line@3.1.0(transitive)
- Removedwrap-ansi@7.0.0(transitive)
- Removedwrappy@1.0.2(transitive)
- Removedxdg-basedir@4.0.0(transitive)
Updatedchalk@^5.0.1
Updatedclipboardy@^3.0.0
Updatedconf@^10.2.0
Updatedupdate-notifier@^6.0.2