fix to use with sveltekit
This commit is contained in:
68
bin/node/pkg.js
Executable file
68
bin/node/pkg.js
Executable file
@@ -0,0 +1,68 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import path from 'node:path'
|
||||
import {pathToFileURL} from 'node:url'
|
||||
import {createWriteStream} from 'node:fs'
|
||||
import {readFile} from 'node:fs/promises'
|
||||
import {promisify} from 'node:util'
|
||||
import child_process from 'node:child_process'
|
||||
|
||||
const exec = promisify(child_process.exec)
|
||||
|
||||
const packageFile = pathToFileURL(path.resolve(process.cwd(), 'package.json'))
|
||||
const packageBuf = await readFile(packageFile)
|
||||
const packageJson = JSON.parse(packageBuf)
|
||||
|
||||
const {
|
||||
dependencies,
|
||||
devDependencies,
|
||||
} = packageJson
|
||||
|
||||
let cc = 0
|
||||
|
||||
function _error(message) {
|
||||
process.stderr.write(message)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
function getLatestVersionPackage(data, prop) {
|
||||
if (!data) {
|
||||
return Promise.resolve('no data to show')
|
||||
}
|
||||
|
||||
const keys = Object.keys(data)
|
||||
return Promise.allSettled(
|
||||
keys.map(async name => {
|
||||
const cmd = `npm show ${name} version`
|
||||
let {stdout: version} = await exec(cmd)
|
||||
version = String(version).replace('\n', '')
|
||||
if (version && data[name] !== String(version)) {
|
||||
cc++
|
||||
process.stdout.write(`${name} --> ${version}\n`)
|
||||
packageJson[prop][name] = version
|
||||
return {name, version}
|
||||
}
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
try {
|
||||
await Promise.all([
|
||||
getLatestVersionPackage(dependencies, 'dependencies'),
|
||||
getLatestVersionPackage(devDependencies, 'devDependencies'),
|
||||
])
|
||||
|
||||
createWriteStream(packageFile)
|
||||
.on('finish', () => {
|
||||
process.stdout.write(cc > 0 ? 'All writes are now complete.' : 'No updates')
|
||||
})
|
||||
.on('close', () => {
|
||||
process.exit(0)
|
||||
})
|
||||
.on('error', error => {
|
||||
_error(error.message)
|
||||
})
|
||||
.end(`${JSON.stringify(packageJson, undefined, ' ')}\n`)
|
||||
} catch (error) {
|
||||
_error(error.message)
|
||||
}
|
||||
94
bin/node/zera
Executable file
94
bin/node/zera
Executable file
@@ -0,0 +1,94 @@
|
||||
#!/bin/bash
|
||||
|
||||
DIR="$(cd $(dirname $0) && pwd)"
|
||||
BIN_DIR="$(cd $DIR/.. && pwd)"
|
||||
|
||||
# Import functions
|
||||
. $BIN_DIR/helper/fn
|
||||
|
||||
# Usage
|
||||
usage() {
|
||||
cat <<-EOF
|
||||
|
||||
Usage: $0 [options...]
|
||||
|
||||
Options:
|
||||
-m, --manager <manager> Package manager: npm (default), yarn or pnpm
|
||||
-s, --shame Shamefully hoist (only pnpm)
|
||||
-h, --help Show usage
|
||||
|
||||
EOF
|
||||
exit 2
|
||||
}
|
||||
|
||||
unset CURR_FOLDER
|
||||
CURR_FOLDER="$(pwd)"
|
||||
|
||||
unset ACTION
|
||||
ACTION=npm
|
||||
|
||||
unset SHAME
|
||||
SHAME=""
|
||||
|
||||
unset VERBOSE
|
||||
VERBOSE=false
|
||||
|
||||
unset CMD_AVAILABLE
|
||||
CMD_AVAILABLE=("npm" "yarn" "pnpm")
|
||||
|
||||
for i in "$@"; do
|
||||
case $i in
|
||||
-h|--help)
|
||||
usage
|
||||
;;
|
||||
-m|--manager)
|
||||
ACTION="${2}"
|
||||
shift
|
||||
;;
|
||||
-s|--shame)
|
||||
SHAME="--shamefully-hoist"
|
||||
shift
|
||||
;;
|
||||
-*|--*)
|
||||
abort "Unknown option $i"
|
||||
;;
|
||||
*)
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Check command available
|
||||
[[ ! " ${CMD_AVAILABLE[*]} " =~ " ${ACTION} " ]] && usage
|
||||
|
||||
# Go to current directory
|
||||
cd $CURR_FOLDER
|
||||
|
||||
# Remove current data
|
||||
rm -rf \
|
||||
"${CURR_FOLDER}/node_modules" \
|
||||
"${CURR_FOLDER}/package-lock.json" \
|
||||
"${CURR_FOLDER}/yarn.lock" \
|
||||
"${CURR_FOLDER}/.yarn" \
|
||||
"${CURR_FOLDER}/.yarnrc.yml" \
|
||||
"${CURR_FOLDER}/.pnp.*" \
|
||||
"${CURR_FOLDER}/pnpm-lock.yaml"
|
||||
|
||||
# Generate .yarnrc.yml
|
||||
if [ "${ACTION}" = "yarn" ]; then
|
||||
echo "" > "${CURR_FOLDER}/.yarnrc.yml"
|
||||
yarn set version stable
|
||||
echo "nodeLinker: node-modules" >> "${CURR_FOLDER}/.yarnrc.yml"
|
||||
fi
|
||||
|
||||
# Install packages
|
||||
if [ "${ACTION}" = "pnpm" ]; then
|
||||
$ACTION install $SHAME
|
||||
else
|
||||
$ACTION install
|
||||
fi
|
||||
|
||||
# Audit packages
|
||||
test $? -ne 0 && abort "${ACTION} failed" || ok "zerado..."
|
||||
test "${ACTION}" = "yarn" && yarn npm audit || $ACTION audit
|
||||
|
||||
exit 0
|
||||
Reference in New Issue
Block a user