diff --git a/.github/workflows/llvm-build-bump-pr.yml b/.github/workflows/llvm-build-bump-pr.yml index ce745f2..256c980 100644 --- a/.github/workflows/llvm-build-bump-pr.yml +++ b/.github/workflows/llvm-build-bump-pr.yml @@ -407,7 +407,7 @@ jobs: - name: Download artifacts for clang-format-git-python uses: actions/download-artifact@v4 with: - path: packages/clang-format-git-python/src + path: packages/clang-format-git-python/src/script name: git-clang-format - name: Download artifacts for clang-format-node diff --git a/package-lock.json b/package-lock.json index 6d54101..8827422 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15289,8 +15289,7 @@ "clang-format-node": "^1.1.3" }, "bin": { - "clang-format-git": "build/index.js", - "git-clang-format": "build/index.js" + "clang-format-git": "build/index.js" }, "engines": { "node": ">=16" @@ -15304,8 +15303,8 @@ "clang-format-node": "^1.1.3" }, "bin": { - "clang-format-git-python": "build/git-clang-format", - "git-clang-format-python": "build/git-clang-format" + "clang-format-git-python": "build/cli.js", + "git-clang-format": "build/cli.js" }, "engines": { "node": ">=16" diff --git a/packages/clang-format-git-python/package.json b/packages/clang-format-git-python/package.json index 3814c86..bb9110a 100644 --- a/packages/clang-format-git-python/package.json +++ b/packages/clang-format-git-python/package.json @@ -2,15 +2,15 @@ "name": "clang-format-git-python", "version": "1.1.3", "description": "Node repackaging of the git-clang-format Python script. This package requires Python3 as a dependency.🐉", - "main": "build/git-clang-format", + "main": "build/index.js", "files": [ "build", "LICENSE", "README.md" ], "bin": { - "git-clang-format-python": "build/git-clang-format", - "clang-format-git-python": "build/git-clang-format" + "git-clang-format": "build/cli.js", + "clang-format-git-python": "build/cli.js" }, "keywords": [ "clang-format", @@ -44,10 +44,12 @@ "node": ">=16" }, "scripts": { - "postinstall": "chmod 755 ./build/git-clang-format || true", + "postinstall": "test -d src && npm run build || npm run chmod", "prepublishOnly": "npm run build", - "build": "cp -r src build && cp ../../LICENSE ../../README.md .", - "test": "npx mocha ./tests --inline-diffs true || true" + "build": "npx babel src -d build --ignore **/*.test.js && cp -r src/script build && cp ../../LICENSE ../../README.md .", + "postbuild": "npm run chmod", + "test": "node --test", + "chmod": "find ./src/script ./build/script -type f -exec chmod 755 {} + || true" }, "dependencies": { "clang-format-node": "^1.1.3" diff --git a/packages/clang-format-git-python/src/cli.js b/packages/clang-format-git-python/src/cli.js new file mode 100644 index 0000000..1407926 --- /dev/null +++ b/packages/clang-format-git-python/src/cli.js @@ -0,0 +1,27 @@ +#!/usr/bin/env node + +const { spawn } = require('child_process'); + +const { clangFormatPath } = require('clang-format-node'); + +const { gitClangFormatPath } = require('./utils/gitClangFormatPath'); + +const spawned = spawn( + 'python', + // Both `--binary=path/to/the/binary` and `--binary path/to/the/binary` commands are valid (the only difference is the `=`). + // + // If you pass a `--binary` argument like `npx git-clang-format-python --binary="path/to/the/binary"` in bash, + // the `--binary="path/to/the/binary"` argument will override the current `--binary=${clangFormatPath}` code. + [gitClangFormatPath, `--binary=${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-git-python/src/cli.test.js b/packages/clang-format-git-python/src/cli.test.js new file mode 100644 index 0000000..e327300 --- /dev/null +++ b/packages/clang-format-git-python/src/cli.test.js @@ -0,0 +1,32 @@ +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', () => { + doesNotThrow(() => { + execSync(`node ${cli}`); // Expected output: 'no modified files to format' + }); + }); + it('node cli.js --help', () => { + doesNotThrow(() => { + execSync(`node ${cli} --help`); + }); + }); + + // Wrong + it('node cli.js --abcdefg', () => { + throws(() => { + execSync(`node ${cli} --abcdefg`); + }); + }); + it('node cli.js --binary=', () => { + throws(() => { + execSync(`node ${cli} --binary=`); + }); + }); +}); diff --git a/packages/clang-format-git-python/src/index.js b/packages/clang-format-git-python/src/index.js new file mode 100644 index 0000000..fe32f64 --- /dev/null +++ b/packages/clang-format-git-python/src/index.js @@ -0,0 +1,9 @@ +const { + gitClangFormatPath, + clangFormatGitPythonPath, +} = require('./utils/gitClangFormatPath'); + +module.exports = { + gitClangFormatPath, + clangFormatGitPythonPath, +}; diff --git a/packages/clang-format-git-python/src/index.test.js b/packages/clang-format-git-python/src/index.test.js new file mode 100644 index 0000000..8a68935 --- /dev/null +++ b/packages/clang-format-git-python/src/index.test.js @@ -0,0 +1,13 @@ +const { ok } = require('node:assert'); +const { describe, it } = require('node:test'); + +const { gitClangFormatPath, clangFormatGitPythonPath } = require('./index'); + +describe('index ok testing', () => { + it('gitClangFormatPath should be imported correctly', () => { + ok(gitClangFormatPath); + }); + it('clangFormatGitPythonPath should be imported correctly', () => { + ok(clangFormatGitPythonPath); + }); +}); diff --git a/packages/clang-format-git-python/src/git-clang-format b/packages/clang-format-git-python/src/script/git-clang-format similarity index 100% rename from packages/clang-format-git-python/src/git-clang-format rename to packages/clang-format-git-python/src/script/git-clang-format diff --git a/packages/clang-format-git-python/src/utils/gitClangFormatPath.js b/packages/clang-format-git-python/src/utils/gitClangFormatPath.js new file mode 100644 index 0000000..b13d301 --- /dev/null +++ b/packages/clang-format-git-python/src/utils/gitClangFormatPath.js @@ -0,0 +1,20 @@ +const { resolve } = require('path'); + +/** + * The ABSOLUTE path to the [`git-clang-format`](../script/git-clang-format) Python script. + * + * @alias `clangFormatGitPythonPath`({@link clangFormatGitPythonPath}) + */ +const gitClangFormatPath = resolve(__dirname, `..`, `script`, `git-clang-format`); + +/** + * The ABSOLUTE path to the [`git-clang-format`](../script/git-clang-format) Python script. + * + * @alias `gitClangFormatPath`({@link gitClangFormatPath}) + */ +const clangFormatGitPythonPath = gitClangFormatPath; + +module.exports = { + gitClangFormatPath, + clangFormatGitPythonPath, +}; diff --git a/packages/clang-format-git-python/src/utils/gitClangFormatPath.test.js b/packages/clang-format-git-python/src/utils/gitClangFormatPath.test.js new file mode 100644 index 0000000..eda6214 --- /dev/null +++ b/packages/clang-format-git-python/src/utils/gitClangFormatPath.test.js @@ -0,0 +1,17 @@ +const { strictEqual } = require('node:assert'); +const { resolve } = require('node:path'); +const { describe, it } = require('node:test'); + +const { gitClangFormatPath, clangFormatGitPythonPath } = require('./gitClangFormatPath'); + +describe('gitClangFormatPath strictEqual testing', () => { + it('gitClangFormatPath === clangFormatGitPythonPath', () => { + strictEqual(gitClangFormatPath, clangFormatGitPythonPath); + }); + it('clangFormatGitPythonPath === resolve(__dirname, `..`, `script`, `git-clang-format`)', () => { + strictEqual( + clangFormatGitPythonPath, + resolve(__dirname, `..`, `script`, `git-clang-format`), + ); + }); +}); diff --git a/packages/clang-format-git/package.json b/packages/clang-format-git/package.json index 7320936..3fa23fc 100644 --- a/packages/clang-format-git/package.json +++ b/packages/clang-format-git/package.json @@ -9,7 +9,6 @@ "README.md" ], "bin": { - "git-clang-format": "build/index.js", "clang-format-git": "build/index.js" }, "keywords": [ diff --git a/packages/clang-format-node/package.json b/packages/clang-format-node/package.json index 877c19c..65bc9b8 100644 --- a/packages/clang-format-node/package.json +++ b/packages/clang-format-node/package.json @@ -41,11 +41,10 @@ "node": ">=16" }, "scripts": { - "postinstall": "npm run chmod", + "postinstall": "test -d src && npm run build || npm run chmod", "prepublishOnly": "npm run build", "build": "npx babel src -d build --ignore **/*.test.js && cp -r src/bin build && cp ../../LICENSE ../../README.md .", "postbuild": "npm run chmod", - "pretest": "npm run chmod", "test": "node --test", "chmod": "find ./src/bin ./build/bin -type f -exec chmod 755 {} + || true" }