1
mirror of https://github.com/jakejarvis/hugo-extended.git synced 2025-10-16 12:14:26 -04:00

Switch to async methods (#128)

This commit is contained in:
XhmikosR
2023-03-17 09:22:09 +02:00
committed by GitHub
parent 9e6b5c865f
commit 9985b1c985
6 changed files with 44 additions and 30 deletions

View File

@@ -5,7 +5,7 @@
"node": true "node": true
}, },
"parserOptions": { "parserOptions": {
"ecmaVersion": 2020, "ecmaVersion": 2022,
"sourceType": "module" "sourceType": "module"
}, },
"extends": "eslint:recommended", "extends": "eslint:recommended",

View File

@@ -1,4 +1,6 @@
import process from 'node:process'; import process from 'node:process';
import lib from './lib/index.js'; import lib from './lib/index.js';
export default lib(process.cwd()).path(); const hugoBin = await lib(process.cwd());
export default hugoBin.path();

View File

@@ -1,12 +1,11 @@
import fs from 'node:fs'; import fs from 'node:fs/promises';
import path from 'node:path'; import path from 'node:path';
import process from 'node:process'; import process from 'node:process';
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
import BinWrapper from '@xhmikosr/bin-wrapper'; import BinWrapper from '@xhmikosr/bin-wrapper';
import { packageConfigSync } from 'pkg-conf'; import { packageConfig } from 'pkg-conf';
const { hugoVersion } = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url)));
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 destDir = path.join(fileURLToPath(new URL('../vendor', import.meta.url)));
const binName = process.platform === 'win32' ? 'hugo.exe' : 'hugo'; const binName = process.platform === 'win32' ? 'hugo.exe' : 'hugo';
@@ -41,11 +40,10 @@ const normalBin = (baseDownloadUrl) => new BinWrapper()
.dest(destDir) .dest(destDir)
.use(binName); .use(binName);
function main(projectRoot) { async function main(projectRoot) {
const config = packageConfigSync('hugo-bin', { cwd: 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 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 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}/`; const baseDownloadUrl = `${downloadRepo}/gohugoio/hugo/releases/download/v${hugoVersion}/`;
return extended ? extendedBin(baseDownloadUrl) : normalBin(baseDownloadUrl); return extended ? extendedBin(baseDownloadUrl) : normalBin(baseDownloadUrl);

View File

@@ -1,7 +1,7 @@
import path from 'node:path'; import path from 'node:path';
import process from 'node:process'; import process from 'node:process';
import picocolors from 'picocolors'; import picocolors from 'picocolors';
import bin from './index.js'; import hugoBin from './index.js';
function getProjectRoot() { function getProjectRoot() {
// `projectRoot` on postinstall could be INIT_CWD introduced in npm >= 5.4 // `projectRoot` on postinstall could be INIT_CWD introduced in npm >= 5.4
@@ -23,10 +23,17 @@ function getProjectRoot() {
return cwd; return cwd;
} }
bin(getProjectRoot()).run(['version']) async function main() {
.then(() => { const projectRoot = getProjectRoot();
const bin = await hugoBin(projectRoot);
bin.run(['version']).then(() => {
console.log(picocolors.green('Hugo binary successfully installed!')); console.log(picocolors.green('Hugo binary successfully installed!'));
}) })
.catch(error => { .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();

View File

@@ -14,7 +14,9 @@
"author": "satoshun00 <shun.sato@fenneclab.com>", "author": "satoshun00 <shun.sato@fenneclab.com>",
"license": "MIT", "license": "MIT",
"type": "module", "type": "module",
"exports": "./index.js", "exports": {
".": "./index.js"
},
"bin": { "bin": {
"hugo": "cli.js" "hugo": "cli.js"
}, },

View File

@@ -5,10 +5,9 @@ import { test, suite } from 'uvu';
import hugoBin from '../index.js'; import hugoBin from '../index.js';
import hugoLib from '../lib/index.js'; import hugoLib from '../lib/index.js';
test('should return path to binary and work', () => { test('should return path to binary and work', async () => {
return binCheck(hugoBin, ['version']).then(works => { const works = await binCheck(hugoBin, ['version']);
assert.ok(works); assert.equal(works, true);
});
}); });
test.run(); test.run();
@@ -32,26 +31,29 @@ hugoLibCustomRepoTestSuite('verify test env', () => {
// Default Repository - Test Cases // Default Repository - Test Cases
hugoLibCustomRepoTestSuite('should return default repository url - Repository: default - Extended: undefined', () => { hugoLibCustomRepoTestSuite('should return default repository url - Repository: default - Extended: undefined', async () => {
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) { for (const sourceUrl of repoSources) {
assert.equal(sourceUrl.startsWith('https://github.com/'), true); 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 = ''; 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) { for (const sourceUrl of repoSources) {
assert.equal(sourceUrl.startsWith('https://github.com/'), true); 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'; 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) { for (const sourceUrl of repoSources) {
assert.equal(sourceUrl.startsWith('https://github.com/'), true); assert.equal(sourceUrl.startsWith('https://github.com/'), true);
@@ -60,29 +62,32 @@ hugoLibCustomRepoTestSuite('should return default repository url - Repository: d
// Custom/Enterprise Repository Test Cases // 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'; 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) { for (const sourceUrl of repoSources) {
assert.equal(sourceUrl.startsWith('https://some1.example.com/'), true); 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_build_tags = '';
process.env.npm_config_hugo_bin_download_repo = 'https://some2.example.com'; 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) { for (const sourceUrl of repoSources) {
assert.equal(sourceUrl.startsWith('https://some2.example.com/'), true); 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_build_tags = 'extended';
process.env.npm_config_hugo_bin_download_repo = 'https://some3.example.com'; 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) { for (const sourceUrl of repoSources) {
assert.equal(sourceUrl.startsWith('https://some3.example.com/'), true); assert.equal(sourceUrl.startsWith('https://some3.example.com/'), true);