diff --git a/.eslintrc.json b/.eslintrc.json index 8342841..ac0d50e 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -5,7 +5,7 @@ "node": true }, "parserOptions": { - "ecmaVersion": 2020, + "ecmaVersion": 2022, "sourceType": "module" }, "extends": "eslint:recommended", diff --git a/index.js b/index.js index 53cdd51..0aa8560 100644 --- a/index.js +++ b/index.js @@ -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(); diff --git a/lib/index.js b/lib/index.js index d973eea..b4a9162 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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'; @@ -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); diff --git a/lib/install.js b/lib/install.js index 65e675b..82db14a 100644 --- a/lib/install.js +++ b/lib/install.js @@ -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 @@ -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(); diff --git a/package.json b/package.json index ec1c69d..783e7ab 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,9 @@ "author": "satoshun00 ", "license": "MIT", "type": "module", - "exports": "./index.js", + "exports": { + ".": "./index.js" + }, "bin": { "hugo": "cli.js" }, diff --git a/test/index.js b/test/index.js index aeffbe1..c0b85a1 100644 --- a/test/index.js +++ b/test/index.js @@ -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(); @@ -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); @@ -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);