diff --git a/package-lock.json b/package-lock.json index 91c8d1a..9d7f45e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15614,7 +15614,7 @@ "hasInstallScript": true, "license": "MIT", "bin": { - "clang-format": "build/index.js" + "clang-format": "build/cli.js" }, "engines": { "node": ">=16" diff --git a/package.json b/package.json index 6a31e82..ec39dc3 100644 --- a/package.json +++ b/package.json @@ -42,9 +42,6 @@ "npx editorconfig-checker -config .editorconfig-checker.json" ], "*.js": "npx eslint", - "*.md": "npx markdownlint", - "packages/**/*.js": [ - "npm run test" - ] + "*.md": "npx markdownlint" } } diff --git a/packages/clang-format-node/package.json b/packages/clang-format-node/package.json index 65e34f4..4029e63 100644 --- a/packages/clang-format-node/package.json +++ b/packages/clang-format-node/package.json @@ -9,7 +9,7 @@ "README.md" ], "bin": { - "clang-format": "build/index.js" + "clang-format": "build/cli.js" }, "keywords": [ "clang-format", @@ -43,7 +43,8 @@ "scripts": { "postinstall": "find ./build/bin -type f -exec chmod 755 {} + || true", "prepublishOnly": "npm run build", - "build": "npx babel src -d build --no-comments --compact true --minified && cp -r src/bin build && cp ../../LICENSE ../../README.md .", - "test": "npx mocha ./tests --inline-diffs true" + "build": "npx babel src -d build --ignore **/*.test.js && cp -r src/bin build && cp ../../LICENSE ../../README.md .", + "pretest": "find ./src/bin -type f -exec chmod 755 {} + || true", + "test": "node --test" } } diff --git a/packages/clang-format-node/src/cli.js b/packages/clang-format-node/src/cli.js new file mode 100644 index 0000000..e61dc23 --- /dev/null +++ b/packages/clang-format-node/src/cli.js @@ -0,0 +1,18 @@ +#!/usr/bin/env node +// The shebang line `#!/usr/bin/env node` ensures the script runs with the correct Node.js interpreter across different environments. + +const { spawn } = require('child_process'); + +const { clangFormatPath } = require('./utils/clangFormatPath'); + +const spawned = spawn(clangFormatPath, process.argv.slice(2), { + stdio: 'inherit', +}); + +spawned.on('close', code => { + if (code !== 0) { + // eslint-disable-next-line no-console + console.error(`Process exited with code: ${code}`); + process.exit(code); + } +}); diff --git a/packages/clang-format-node/src/cli.test.js b/packages/clang-format-node/src/cli.test.js new file mode 100644 index 0000000..f178e48 --- /dev/null +++ b/packages/clang-format-node/src/cli.test.js @@ -0,0 +1,26 @@ +const { doesNotThrow, throws } = require('node:assert'); +const { execSync } = require('node:child_process'); +const { resolve } = require('node:path'); +const { describe, it } = require('node:test'); + +const cli = resolve(__dirname, 'cli.js'); + +describe('cli doesNotThrow and throws testing', () => { + // Correct + it('node cli.js --help', () => { + doesNotThrow(() => { + execSync(`node ${cli} --help`); + }); + }); + it('node cli.js --version', () => { + doesNotThrow(() => { + execSync(`node ${cli} --version`); + }); + }); + // Wrong + it('node cli.js --abcdefg', () => { + throws(() => { + execSync(`node ${cli} --abcdefg`); + }); + }); +}); diff --git a/packages/clang-format-node/src/getClangFormatPath.js b/packages/clang-format-node/src/getClangFormatPath.js deleted file mode 100644 index 6410d19..0000000 --- a/packages/clang-format-node/src/getClangFormatPath.js +++ /dev/null @@ -1,27 +0,0 @@ -const { existsSync } = require('fs'); -const { resolve } = require('path'); - -/** - * Resolves and returns the path to the `clang-format` executable based on the OS platform and architecture. - * Throws an error if the executable is not found. - * - * @param {string} osPlatform The current operating system platform (e.g., 'darwin', 'linux', 'win32'). - * @param {string} architecture The system architecture (e.g., 'arm64', 'x64'). - * @returns {string} The resolved path to the `clang-format` executable. - * @throws {Error} Throws an error if the executable is not found for the specified OS platform and architecture. - */ -module.exports = (osPlatform, architecture) => { - const clangFormatPath = resolve( - __dirname, - `bin`, - `cfn-${osPlatform}-${architecture}`, - `clang-format${osPlatform === 'win32' ? '.exe' : ''}`, - ); - - if (!existsSync(clangFormatPath)) - throw new Error( - `No executable found for '${osPlatform}(OS platform)-${architecture}(architecture)'\nSee supported OS platform and architecture lists of clang-format-node in README.md`, - ); - - return clangFormatPath; -}; diff --git a/packages/clang-format-node/src/index.js b/packages/clang-format-node/src/index.js index 1c351c4..395701d 100644 --- a/packages/clang-format-node/src/index.js +++ b/packages/clang-format-node/src/index.js @@ -1,26 +1,7 @@ -#!/usr/bin/env node -// The shebang line `#!/usr/bin/env node` ensures the script runs with the correct Node.js interpreter across different environments. +const { clangFormatPath } = require('./utils/clangFormatPath'); +const { getClangFormatPath } = require('./utils/getClangFormatPath'); -const { platform, arch } = require('os'); -const { spawn } = require('child_process'); -const getClangFormatPath = require('./getClangFormatPath'); - -const OS_PLATFORM = platform(); -const ARCHITECTURE = arch(); - -try { - const spawned = spawn( - getClangFormatPath(OS_PLATFORM, ARCHITECTURE), - process.argv.slice(2), - { - stdio: 'inherit', - }, - ); - - // Terminate the parent process after the child process has completed. - spawned.on('close', process.exit); -} catch (error) { - // eslint-disable-next-line no-console - console.error(error.message); - process.exit(1); -} +module.exports = { + clangFormatPath, + getClangFormatPath, +}; diff --git a/packages/clang-format-node/src/index.test.js b/packages/clang-format-node/src/index.test.js new file mode 100644 index 0000000..e283b0d --- /dev/null +++ b/packages/clang-format-node/src/index.test.js @@ -0,0 +1,13 @@ +const { ok } = require('node:assert'); +const { describe, it } = require('node:test'); + +const { clangFormatPath, getClangFormatPath } = require('./index'); + +describe('index ok testing', () => { + it('clangFormatPath should be imported correctly', () => { + ok(clangFormatPath); + }); + it('getClangFormatPath should be imported correctly', () => { + ok(getClangFormatPath); + }); +}); diff --git a/packages/clang-format-node/src/utils/clangFormatPath.js b/packages/clang-format-node/src/utils/clangFormatPath.js new file mode 100644 index 0000000..6282ae0 --- /dev/null +++ b/packages/clang-format-node/src/utils/clangFormatPath.js @@ -0,0 +1,12 @@ +const { platform, arch } = require('os'); + +const { getClangFormatPath } = require('./getClangFormatPath'); + +/** + * The ABSOLUTE path to the `clang-format` executable binary based on the OS platform and architecture. + */ +const clangFormatPath = getClangFormatPath(platform(), arch()); + +module.exports = { + clangFormatPath, +}; diff --git a/packages/clang-format-node/src/utils/clangFormatPath.test.js b/packages/clang-format-node/src/utils/clangFormatPath.test.js new file mode 100644 index 0000000..7c6ac3f --- /dev/null +++ b/packages/clang-format-node/src/utils/clangFormatPath.test.js @@ -0,0 +1,12 @@ +const { strictEqual } = require('node:assert'); +const { platform, arch } = require('node:os'); +const { describe, it } = require('node:test'); + +const { clangFormatPath } = require('./clangFormatPath'); +const { getClangFormatPath } = require('./getClangFormatPath'); + +describe('clangFormatPath strictEqual testing', () => { + it('clangFormatPath === getClangFormatPath(platform(), arch())', () => { + strictEqual(clangFormatPath, getClangFormatPath(platform(), arch())); + }); +}); diff --git a/packages/clang-format-node/src/utils/getClangFormatPath.js b/packages/clang-format-node/src/utils/getClangFormatPath.js new file mode 100644 index 0000000..8759eab --- /dev/null +++ b/packages/clang-format-node/src/utils/getClangFormatPath.js @@ -0,0 +1,35 @@ +const { existsSync } = require('fs'); +const { resolve } = require('path'); + +/** + * Returns the ABSOLUTE path to the `clang-format` executable binary based on the OS platform and architecture. + * + * The possible combinations are `darwin-arm64`, `darwin-x64`, `linux-arm`, `linux-arm64`, `linux-ppc64`, `linux-s390x`, `linux-x64`, `win32-x64`. + * + * Throws an error if the executable is not found. + * + * @param {string} osPlatform The current operating system platform. (e.g., `darwin`, `linux`, `win32`) + * @param {string} architecture The current system architecture. (e.g., `arm`, `arm64`, `ppc64`, `s390x`, `x64`) + * @returns {string} The ABSOLUTE path to the `clang-format` executable binary. + * @throws `Error` Throws an error if the executable binary is not found for the specified OS platform and architecture. + */ +function getClangFormatPath(osPlatform, architecture) { + const clangFormatPath = resolve( + __dirname, + `..`, + `bin`, + `cfn-${osPlatform}-${architecture}`, + `clang-format${osPlatform === 'win32' ? '.exe' : ''}`, + ); + + if (!existsSync(clangFormatPath)) + throw new Error( + `No executable found for '${osPlatform}(OS platform)-${architecture}(architecture)'\nThe possible combinations are 'darwin-arm64', 'darwin-x64', 'linux-arm', 'linux-arm64', 'linux-ppc64', 'linux-s390x', 'linux-x64', 'win32-x64'`, + ); + + return clangFormatPath; +} + +module.exports = { + getClangFormatPath, +}; diff --git a/packages/clang-format-node/tests/getClangFormatPath.spec.js b/packages/clang-format-node/src/utils/getClangFormatPath.test.js similarity index 64% rename from packages/clang-format-node/tests/getClangFormatPath.spec.js rename to packages/clang-format-node/src/utils/getClangFormatPath.test.js index dc43264..1a9be39 100644 --- a/packages/clang-format-node/tests/getClangFormatPath.spec.js +++ b/packages/clang-format-node/src/utils/getClangFormatPath.test.js @@ -1,9 +1,16 @@ -const { doesNotThrow, throws } = require('assert'); -const getClangFormatPath = require('../src/getClangFormatPath'); +const { doesNotThrow, throws } = require('node:assert'); +const { describe, it } = require('node:test'); -// See possible values in https://nodejs.org/api/os.html#osplatform +const { getClangFormatPath } = require('./getClangFormatPath'); + +/** + * See possible values in {@link https://nodejs.org/api/os.html#osplatform}. + */ const osPlatforms = ['aix', 'darwin', 'freebsd', 'linux', 'openbsd', 'sunos', 'win32']; -// See possible values in https://nodejs.org/api/os.html#osarch + +/** + * See possible values in {@link https://nodejs.org/api/os.html#osarch}. + */ const architectures = [ 'arm', 'arm64', @@ -18,15 +25,18 @@ const architectures = [ 's390x', 'x64', ]; + +/** + * The possible combinations are `darwin-arm64`, `darwin-x64`, `linux-arm`, `linux-arm64`, `linux-ppc64`, `linux-s390x`, `linux-x64`, `win32-x64`. + * + * See {@link getClangFormatPath}. + */ const allowed = { darwin: ['arm64', 'x64'], linux: ['arm', 'arm64', 'ppc64', 's390x', 'x64'], win32: ['x64'], }; -/** - * Tests for the `getClangFormatPath` - */ describe('getClangFormatPath doesNotThrow and throws testing', () => { osPlatforms.forEach(osPlatform => { architectures.forEach(architecture => {