From 19e34d8ed66f9f0c98ab3da17fd187f613fa5d48 Mon Sep 17 00:00:00 2001 From: CismonX Date: Wed, 21 Apr 2021 17:25:24 +0800 Subject: [PATCH] Refactor build scripts. --- .gitignore | 1 + .vscodeignore | 2 +- package.json | 5 ++++- scripts/make-vsix.js | 33 +++++++++++++++++++++++++++++++ scripts/package.sh | 47 +++++++++++++++++++------------------------- tsconfig.json | 7 ++++--- 6 files changed, 63 insertions(+), 32 deletions(-) create mode 100644 scripts/make-vsix.js diff --git a/.gitignore b/.gitignore index ca21d50..d029e5a 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ .vscode/ node_modules/ out/ +texinfo-*.d/ diff --git a/.vscodeignore b/.vscodeignore index 006294a..c27ede6 100644 --- a/.vscodeignore +++ b/.vscodeignore @@ -8,7 +8,6 @@ **/*.map **/*.ts -**/*.vsce-pre-package-backup .drone.yml .gitignore .gitattributes @@ -19,4 +18,5 @@ LICENSE_NOTICE.md node_modules/ src/ scripts/ +texinfo-*.d/ tsconfig.json diff --git a/package.json b/package.json index 32dd047..2c6bc8e 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,10 @@ }, "eslintConfig": { "root": true, - "ignorePatterns": "out/**", + "ignorePatterns": [ + "out/**", + "scripts/**" + ], "parser": "@typescript-eslint/parser", "plugins": [ "@typescript-eslint" diff --git a/scripts/make-vsix.js b/scripts/make-vsix.js new file mode 100644 index 0000000..420bed7 --- /dev/null +++ b/scripts/make-vsix.js @@ -0,0 +1,33 @@ +/** + * Copyright (C) 2021 CismonX + * + * Copying and distribution of this file, with or without modification, are + * permitted in any medium without royalty, provided the copyright notice and + * this notice are preserved. This file is offered as-is, without any warranty. + */ + +const fs = require('fs') +const yazl = require('yazl'); + +async function addPathToZipFile(zipFile, path) { + for (file of await fs.promises.readdir(path, { withFileTypes: true })) { + const name = `${path}/${file.name}`; + if (file.isDirectory()) { + await addPathToZipFile(zipFile, name); + } else { + zippedPath = name.substr(name.indexOf('/') + 1); + console.log(`+ ${zippedPath}`); + zipFile.addFile(name, zippedPath); + } + } +} + +(async () => { + const vsixFile = process.argv.pop(); + console.log(`Creating ${vsixFile}:`); + const zipFile = new yazl.ZipFile(); + zipFile.outputStream.pipe(fs.createWriteStream(vsixFile)) + .on('close', () => console.log(`Finish creating ${vsixFile}`)); + await addPathToZipFile(zipFile, `${vsixFile}.d`); + zipFile.end(); +})() diff --git a/scripts/package.sh b/scripts/package.sh index 4ac6b5d..5aebea9 100755 --- a/scripts/package.sh +++ b/scripts/package.sh @@ -7,31 +7,24 @@ # this notice are preserved. This file is offered as-is, without any warranty. # -BACKUP_SUFFIX=vsce-pre-package-backup -JSON_FILES=(package.json language-configuration.json) -for file in ${JSON_FILES[@]}; do - mv $file $file.$BACKUP_SUFFIX - json5 -o $file $file.$BACKUP_SUFFIX -done -XML_PATH=./node_modules/vsce/resources -XML_FILES=($XML_PATH/\[Content_Types\].xml $XML_PATH/extension.vsixmanifest) -for file in ${XML_FILES[@]}; do - mv $file $file.$BACKUP_SUFFIX - minify-xml --output $file $file.$BACKUP_SUFFIX -done -MD_FILES=(README.md CHANGELOG.md) -for file in ${MD_FILES[@]}; do - mv $file $file.$BACKUP_SUFFIX - tail -n +9 $file.$BACKUP_SUFFIX > $file -done -json -j0 -I -e "$(cat ./scripts/package-json-cleanup.js)" -f package.json +VSIX_FILE_NAME=texinfo-$(json -f package.json version).vsix +PACKAGE_JSON_CLEANUP_JS=$(cat ./scripts/package-json-cleanup.js) + vsce package --baseContentUrl=$(json -f package.json repository.url)/tree -for file in ${JSON_FILES[@]}; do - mv $file.$BACKUP_SUFFIX $file -done -for file in ${MD_FILES[@]}; do - mv $file.$BACKUP_SUFFIX $file -done -for file in ${XML_FILES[@]}; do - mv $file.$BACKUP_SUFFIX $file -done + +unzip -d $VSIX_FILE_NAME{.d,} +cd $VSIX_FILE_NAME.d +minify-xml --output \[Content_Types\].xml{,} +minify-xml --output extension.vsixmanifest{,} +cd extension +# Minify JSON files. +json -j0 -I -e "$PACKAGE_JSON_CLEANUP_JS" -f package.json +json5 -o package.json{,} +json5 -o language-configuration.json{,} +# Remove comments from Markdown files. +sed -i '' -e '1,8d' README.md CHANGELOG.md +cd ../.. + +# Re-package .vsix file. +node ./scripts/make-vsix.js $VSIX_FILE_NAME +rm -r $VSIX_FILE_NAME.d diff --git a/tsconfig.json b/tsconfig.json index 8cf20b8..3a47c51 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,10 +15,11 @@ "sourceMap": true, "rootDir": "src", "strict": true, - "noImplicitReturns": true + "noImplicitReturns": true, }, "exclude": [ + "*.config.ts", "node_modules", - "*.config.ts" - ] + "scripts", + ], }