Skip to content

Commit

Permalink
chore: download humctl in node
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswuerbach committed Jun 20, 2024
1 parent ffad173 commit a1813fc
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 29 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
- run: npm ci

- run: npm run lint
if: runner.os == 'Linux'

- run: xvfb-run -a npm test
if: runner.os == 'Linux'
Expand Down
14 changes: 14 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended';
import globals from 'globals';

export default tseslint.config(
eslint.configs.recommended,
Expand All @@ -22,6 +23,19 @@ export default tseslint.config(
},
files: ['src/test/**/*.test.ts'],
},
{
name: 'scripts',
languageOptions: {
globals: {
...globals.node,
},
},
rules: {
// scripts are plain js
'@typescript-eslint/no-require-imports': 'off',
},
files: ['scripts/*.js'],
},
eslintPluginPrettierRecommended,
{
ignores: ['out/', 'dist/', '.vscode-test/'],
Expand Down
148 changes: 143 additions & 5 deletions package-lock.json

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

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@
}
},
"scripts": {
"preinstall": "./scripts/download-humctl.sh",
"preinstall": "node ./scripts/download-humctl.js",
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"pretest": "npm run compile && npm run lint",
"pretest": "npm run compile",
"lint": "eslint .",
"test": "vscode-test",
"prettier": "npx prettier . --write",
Expand All @@ -185,12 +185,14 @@
"eslint": "^9.5.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"extract-zip": "^2.0.1",
"husky": "^9.0.11",
"lint-staged": "^15.2.7",
"mocha": "^10.4.0",
"prettier": "^3.3.2",
"sinon": "^18.0.0",
"sinon-chai": "^3.7.0",
"tar": "^7.4.0",
"typescript": "^5.4.5",
"typescript-eslint": "^8.0.0-alpha.30",
"wait-for-expect": "^3.0.2"
Expand Down
87 changes: 87 additions & 0 deletions scripts/download-humctl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env node

const CLI_VERSION = '0.25.1';

const fs = require('node:fs/promises');
const extractZip = require('extract-zip');
const tar = require('tar');
const { tmpdir } = require('node:os');
const path = require('node:path');

const download = async (url, dest) => {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`unexpected response ${response.statusText}`);
}

const buffer = await response.arrayBuffer();
await fs.writeFile(dest, Buffer.from(buffer));
};

const downloadAndExtract = async (url, file, dest, archiveSuffix, extract) => {
const tmpDir = await fs.mkdtemp(path.join(tmpdir(), 'vscode-humanitec-'));
try {
const archiveFile = `${tmpDir}/archive${archiveSuffix}`;
await download(url, archiveFile);
await extract(archiveFile, tmpDir);
await fs.copyFile(`${tmpDir}/${file}`, dest);
} finally {
await fs.rm(tmpDir, { recursive: true, force: true });
}
};

const downloadAndExtractGzip = (url, file, dest) => {
return downloadAndExtract(url, file, dest, '.tar.gz', (archiveFile, tmpDir) =>
tar.extract({
file: archiveFile,
cwd: tmpDir,
})
);
};

const downloadAndExtractZip = async (url, file, dest) => {
return downloadAndExtract(url, file, dest, '.zip', (archiveFile, tmpDir) =>
extractZip(archiveFile, { dir: tmpDir })
);
};

const main = async () => {
const humctlDir = './humctl';
await fs.rm(humctlDir, { recursive: true, force: true });
await fs.mkdir(humctlDir, { recursive: true });

const prefix = `https://github.com/humanitec/cli/releases/download/v${CLI_VERSION}/cli_${CLI_VERSION}`;

await Promise.all([
downloadAndExtractGzip(
`${prefix}_darwin_amd64.tar.gz`,
'humctl',
`${humctlDir}/cli_${CLI_VERSION}_darwin_x64`
),
downloadAndExtractGzip(
`${prefix}_darwin_arm64.tar.gz`,
'humctl',
`${humctlDir}/cli_${CLI_VERSION}_darwin_arm64`
),
downloadAndExtractGzip(
`${prefix}_linux_amd64.tar.gz`,
'humctl',
`${humctlDir}/cli_${CLI_VERSION}_linux_x64`
),
downloadAndExtractGzip(
`${prefix}_linux_arm64.tar.gz`,
'humctl',
`${humctlDir}/cli_${CLI_VERSION}_linux_arm64`
),
downloadAndExtractZip(
`${prefix}_windows_amd64.zip`,
'humctl.exe',
`${humctlDir}/cli_${CLI_VERSION}_win32_x64.exe`
),
]);
};

main().catch(err => {
console.error(err);
process.exit(1);
});
Loading

0 comments on commit a1813fc

Please sign in to comment.