Skip to content

Commit

Permalink
feat(clang-format-git-python): create new package named `clang-format…
Browse files Browse the repository at this point in the history
…-git-python` (#79)

A new feature has arrived.

`clang-format-git-python` is now available.

See: #60 

* wip: update root's `package-lock.json` and package's `package.json`

* rename: change the directory of `git-clang-format`

* wip: create `clangFormatGitPath.js`

* wip: create `index.js`

* wip: create `cli.js`

* test: create `index.test.js`

* test: create `clangFormatGitPath.test.js`

* test: create `cli.test.js`

* ci: change the target path of download-artifact in `llvm-build-bump-pr`

* chore: update `postinstall` script and delete `pretest` script

* rename: `clangFormatGitPath.js` to `gitClangFormatPath.js`

* style: change the order of `gitClangFormatPath` and `clangFormatGitPath`

* rename: `git-clang-format-python` to `git-clang-format`

* rename: variable `clangFormatGitPath` to `clangFormatGitPythonPath`

* chore: update `package-lock.json`

* wip
  • Loading branch information
lumirlumir authored Nov 6, 2024
1 parent 83aae8c commit 063411e
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/llvm-build-bump-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions packages/clang-format-git-python/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
Expand Down
27 changes: 27 additions & 0 deletions packages/clang-format-git-python/src/cli.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
32 changes: 32 additions & 0 deletions packages/clang-format-git-python/src/cli.test.js
Original file line number Diff line number Diff line change
@@ -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=`);
});
});
});
9 changes: 9 additions & 0 deletions packages/clang-format-git-python/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const {
gitClangFormatPath,
clangFormatGitPythonPath,
} = require('./utils/gitClangFormatPath');

module.exports = {
gitClangFormatPath,
clangFormatGitPythonPath,
};
13 changes: 13 additions & 0 deletions packages/clang-format-git-python/src/index.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
20 changes: 20 additions & 0 deletions packages/clang-format-git-python/src/utils/gitClangFormatPath.js
Original file line number Diff line number Diff line change
@@ -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,
};
Original file line number Diff line number Diff line change
@@ -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`),
);
});
});
1 change: 0 additions & 1 deletion packages/clang-format-git/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"README.md"
],
"bin": {
"git-clang-format": "build/index.js",
"clang-format-git": "build/index.js"
},
"keywords": [
Expand Down
3 changes: 1 addition & 2 deletions packages/clang-format-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down

0 comments on commit 063411e

Please sign in to comment.