Skip to content

Commit

Permalink
chore: add typechecking for javascript scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Jun 13, 2020
1 parent c745ad0 commit 78ce23f
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.vscode
*.swp
.env
node_modules
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ This is a loose collection of "scripts" that I have had use for, and that I want

PRs & issues are welcomed, so feel free to ask questions, request help, improve existing stuff (any form of documentation is especially welcomed<3), and point out flaws.

While the majority of scripts are written in shell, there are a few scripts powered by Node/JS.
These require Node 12+, but otherwise have no dependencies - the `package.json` in the repo provides only dev dependencies such as `TypeScript` & `Prettier`.

Some details on the files so far:

### `git-compare-between-branchs`
Expand Down
38 changes: 38 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "scripts",
"version": "1.0.0",
"description": "This is a loose collection of \"scripts\" that I have had use for, and that I want to keep around somewhere just in case.",
"keywords": [],
"homepage": "https://github.com/G-Rath/scripts#readme",
"bugs": {
"url": "https://github.com/G-Rath/scripts/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/G-Rath/scripts.git"
},
"license": "ISC",
"author": "Gareth Jones",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"typecheck": "tsc --noEmit"
},
"prettier": "prettier-config-ackama",
"dependencies": {},
"devDependencies": {
"@schemastore/package": "0.0.5",
"@types/node": "^14.0.13",
"prettier": "^2.0.5",
"prettier-config-ackama": "^0.1.2",
"typescript": "^3.9.5"
}
}
29 changes: 22 additions & 7 deletions src/link_scripts_to_local_bin
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ set +e

SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # iterate until no longer a symlink
DIR="$(cd -P "$(dirname "$SOURCE")" >/dev/null 2>&1 && pwd)"
DIR="$(cd -P "$(dirname "$SOURCE")" > /dev/null 2>&1 && pwd)"
SOURCE="$(readlink "$SOURCE")"

# check if relative symlink
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done
DIR="$(cd -P "$(dirname "$SOURCE")" >/dev/null 2>&1 && pwd)"
DIR="$(cd -P "$(dirname "$SOURCE")" > /dev/null 2>&1 && pwd)"

scripts=(
echo ""
echo "---- linking sh scripts ----"

sh_scripts=(
"wp_cli"
"mknano"
"mktouch"
Expand All @@ -36,15 +39,27 @@ scripts=(
"list_all_file_extensions_in_current_dir"
"docker_run_postgres"
"npm_audit_fix_and_push"
"npm_remove_package_from_lock"
"yarn_remove_package_from_lock"
"yarn_upgrade_and_push"
"npm_update_package_and_commit"
"npm_update_package_and_commit_multiple"
)

for script in "${scripts[@]}"; do
for script in "${sh_scripts[@]}"; do
chmod +x "$DIR/$script"
echo "linking $script"
ln -sf "$DIR/$script" /usr/local/bin/
ln -sf "$DIR/$script" "/usr/local/bin/$script"
done

echo ""
echo "---- linking js scripts ----"

js_scripts=(
"npm_remove_package_from_lock"
"yarn_remove_package_from_lock"
)

for script in "${js_scripts[@]}"; do
chmod +x "$DIR/$script.js"
echo "linking $script.js"
ln -sf "$DIR/$script.js" "/usr/local/bin/$script"
done
8 changes: 6 additions & 2 deletions src/npm_remove_package_from_lock → src/npm_remove_package_from_lock.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#!/usr/bin/env node

/** @type {module:fs} */
const fs = require('fs');

const packagesToRemove = process.argv.slice(2);

/** @type {string} */
let packageLock;

try {
packageLock = JSON.parse(fs.readFileSync('package-lock.json').toString());
} catch {
Expand All @@ -20,10 +24,10 @@ fs.writeFileSync(
(key, value) => {
if (packagesToRemove.includes(key) && typeof value === 'object') {
removedCount++;

return undefined;
}

return value;
},
2
Expand Down
28 changes: 27 additions & 1 deletion src/yarn_remove_package_from_lock → src/yarn_remove_package_from_lock.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env node

/** @type {module:fs} */
const fs = require('fs');

const packagesToRemove = process.argv.slice(2);

/** @type {string} */
let yarnLock;

try {
Expand All @@ -13,6 +14,12 @@ try {
process.exit();
}

/**
* @param {string} lockContents
* @param {string} packageVersionHeader
*
* @return {string}
*/
const findBlockInLock = (lockContents, packageVersionHeader) => {
const lockEntryStart = lockContents.indexOf(packageVersionHeader);

Expand All @@ -34,12 +41,31 @@ const findBlockInLock = (lockContents, packageVersionHeader) => {
);
};

/**
* @param {string} lockContents
* @param {string} entryHeader
*
* @return {string}
*/
const removePackageEntryFromLock = (lockContents, entryHeader) =>
lockContents.replace(findBlockInLock(lockContents, entryHeader), '');

/**
* @param {string} lockContents
* @param {string} packageName
*
* @return {Array<string>}
*/
const findPackageHeadersInLock = (lockContents, packageName) =>
lockContents.match(new RegExp(`^"?${packageName}@.+:`, 'gm')) || [];

/**
*
* @param {string} lockContents
* @param {string} packageName
*
* @return {string}
*/
const removePackageEntriesFromLock = (lockContents, packageName) =>
findPackageHeadersInLock(lockContents, packageName).reduce(
removePackageEntryFromLock,
Expand Down
25 changes: 25 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"target": "ES2019",
"module": "CommonJS",
"moduleResolution": "Node",
"lib": ["ES2020"],
"rootDir": "./",
"baseUrl": "./",
"checkJs": true,
"allowJs": true,
"noEmit": true,
"strict": true,
"resolveJsonModule": true,
"useDefineForClassFields": true,
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noEmitOnError": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"pretty": true,
"sourceMap": true,
"removeComments": false
},
"exclude": ["coverage", "node_modules", "lib"]
}

0 comments on commit 78ce23f

Please sign in to comment.