Skip to content

Commit

Permalink
Switch to async methods (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
XhmikosR authored Mar 17, 2023
1 parent 9e6b5c8 commit 9985b1c
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"node": true
},
"parserOptions": {
"ecmaVersion": 2020,
"ecmaVersion": 2022,
"sourceType": "module"
},
"extends": "eslint:recommended",
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import process from 'node:process';
import lib from './lib/index.js';

export default lib(process.cwd()).path();
const hugoBin = await lib(process.cwd());

export default hugoBin.path();
12 changes: 5 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import fs from 'node:fs';
import fs from 'node:fs/promises';
import path from 'node:path';
import process from 'node:process';
import { fileURLToPath } from 'node:url';
import BinWrapper from '@xhmikosr/bin-wrapper';
import { packageConfigSync } from 'pkg-conf';

const { hugoVersion } = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url)));
import { packageConfig } from 'pkg-conf';

const { hugoVersion } = JSON.parse(await fs.readFile(new URL('../package.json', import.meta.url)));
const destDir = path.join(fileURLToPath(new URL('../vendor', import.meta.url)));
const binName = process.platform === 'win32' ? 'hugo.exe' : 'hugo';

Expand Down Expand Up @@ -41,11 +40,10 @@ const normalBin = (baseDownloadUrl) => new BinWrapper()
.dest(destDir)
.use(binName);

function main(projectRoot) {
const config = packageConfigSync('hugo-bin', { cwd: projectRoot });
async function main(projectRoot) {
const config = await packageConfig('hugo-bin', { cwd: projectRoot });
const extended = (process.env.HUGO_BIN_BUILD_TAGS || process.env.npm_config_hugo_bin_build_tags || config.buildTags) === 'extended';
const downloadRepo = (process.env.HUGO_BIN_DOWNLOAD_REPO || process.env.npm_config_hugo_bin_download_repo || config.downloadRepo || 'https://github.com');

const baseDownloadUrl = `${downloadRepo}/gohugoio/hugo/releases/download/v${hugoVersion}/`;

return extended ? extendedBin(baseDownloadUrl) : normalBin(baseDownloadUrl);
Expand Down
15 changes: 11 additions & 4 deletions lib/install.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'node:path';
import process from 'node:process';
import picocolors from 'picocolors';
import bin from './index.js';
import hugoBin from './index.js';

function getProjectRoot() {
// `projectRoot` on postinstall could be INIT_CWD introduced in npm >= 5.4
Expand All @@ -23,10 +23,17 @@ function getProjectRoot() {
return cwd;
}

bin(getProjectRoot()).run(['version'])
.then(() => {
async function main() {
const projectRoot = getProjectRoot();
const bin = await hugoBin(projectRoot);

bin.run(['version']).then(() => {
console.log(picocolors.green('Hugo binary successfully installed!'));
})
.catch(error => {
console.error(picocolors.red(`${error.message}\nHugo binary installation failed!`));
console.error(picocolors.red('Hugo binary installation failed!'));
throw new Error(error);
});
}

main();
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"author": "satoshun00 <shun.sato@fenneclab.com>",
"license": "MIT",
"type": "module",
"exports": "./index.js",
"exports": {
".": "./index.js"
},
"bin": {
"hugo": "cli.js"
},
Expand Down
37 changes: 21 additions & 16 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import { test, suite } from 'uvu';
import hugoBin from '../index.js';
import hugoLib from '../lib/index.js';

test('should return path to binary and work', () => {
return binCheck(hugoBin, ['version']).then(works => {
assert.ok(works);
});
test('should return path to binary and work', async () => {
const works = await binCheck(hugoBin, ['version']);
assert.equal(works, true);
});

test.run();
Expand All @@ -32,26 +31,29 @@ hugoLibCustomRepoTestSuite('verify test env', () => {

// Default Repository - Test Cases

hugoLibCustomRepoTestSuite('should return default repository url - Repository: default - Extended: undefined', () => {
const repoSources = hugoLib(process.cwd())._src.map((v) => v.url);
hugoLibCustomRepoTestSuite('should return default repository url - Repository: default - Extended: undefined', async () => {
const lib = await hugoLib(process.cwd());
const repoSources = lib._src.map((v) => v.url);

for (const sourceUrl of repoSources) {
assert.equal(sourceUrl.startsWith('https://github.com/'), true);
}
});

hugoLibCustomRepoTestSuite('should return default repository url - Repository: default - Extended: empty', () => {
hugoLibCustomRepoTestSuite('should return default repository url - Repository: default - Extended: empty', async () => {
process.env.npm_config_hugo_bin_build_tags = '';
const repoSources = hugoLib(process.cwd())._src.map((v) => v.url);
const lib = await hugoLib(process.cwd());
const repoSources = lib._src.map((v) => v.url);

for (const sourceUrl of repoSources) {
assert.equal(sourceUrl.startsWith('https://github.com/'), true);
}
});

hugoLibCustomRepoTestSuite('should return default repository url - Repository: default - Extended: extended', () => {
hugoLibCustomRepoTestSuite('should return default repository url - Repository: default - Extended: extended', async () => {
process.env.npm_config_hugo_bin_build_tags = 'extended';
const repoSources = hugoLib(process.cwd())._src.map((v) => v.url);
const lib = await hugoLib(process.cwd());
const repoSources = lib._src.map((v) => v.url);

for (const sourceUrl of repoSources) {
assert.equal(sourceUrl.startsWith('https://github.com/'), true);
Expand All @@ -60,29 +62,32 @@ hugoLibCustomRepoTestSuite('should return default repository url - Repository: d

// Custom/Enterprise Repository Test Cases

hugoLibCustomRepoTestSuite('should return custom repository url - Repository: custom - Extended: undefined', () => {
hugoLibCustomRepoTestSuite('should return custom repository url - Repository: custom - Extended: undefined', async () => {
process.env.npm_config_hugo_bin_download_repo = 'https://some1.example.com';
const repoSources = hugoLib(process.cwd())._src.map((v) => v.url);
const lib = await hugoLib(process.cwd());
const repoSources = lib._src.map((v) => v.url);

for (const sourceUrl of repoSources) {
assert.equal(sourceUrl.startsWith('https://some1.example.com/'), true);
}
});

hugoLibCustomRepoTestSuite('should return custom repository url - Repository: custom - Extended: empty', () => {
hugoLibCustomRepoTestSuite('should return custom repository url - Repository: custom - Extended: empty', async () => {
process.env.npm_config_hugo_bin_build_tags = '';
process.env.npm_config_hugo_bin_download_repo = 'https://some2.example.com';
const repoSources = hugoLib(process.cwd())._src.map((v) => v.url);
const lib = await hugoLib(process.cwd());
const repoSources = lib._src.map((v) => v.url);

for (const sourceUrl of repoSources) {
assert.equal(sourceUrl.startsWith('https://some2.example.com/'), true);
}
});

hugoLibCustomRepoTestSuite('should return custom repository url - Repository: custom - Extended: extended', () => {
hugoLibCustomRepoTestSuite('should return custom repository url - Repository: custom - Extended: extended', async () => {
process.env.npm_config_hugo_bin_build_tags = 'extended';
process.env.npm_config_hugo_bin_download_repo = 'https://some3.example.com';
const repoSources = hugoLib(process.cwd())._src.map((v) => v.url);
const lib = await hugoLib(process.cwd());
const repoSources = lib._src.map((v) => v.url);

for (const sourceUrl of repoSources) {
assert.equal(sourceUrl.startsWith('https://some3.example.com/'), true);
Expand Down

0 comments on commit 9985b1c

Please sign in to comment.