-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(clang-format-node): create new
clangFormatPath
and `getClangFo…
…rmatPath` API (#72) New features have arrived. You can get `clang-format-node` native binary absolute path from `clangFormatPath` and `getClangFormatPath` API. * wip: update root's `package-lock.json` and `clang-format-node`'s `packges.json` * wip: rename and update `getClangFormatPath.js` * chore: update test script for `lint-staged` in `package.json` * test: rename and update test for `getClangFormatPath` I've deleted mocha and replaced it with node.js's test runner. So I renamed it from `spec.js` to `test.js`. * wip: create `clangFormatPath.js` * test: add test for `clangFormatPath` * chore: delete test logic in `lint-staged` of `package.json` * refactor: detach cli logic into `cli.js` * refactor: update `index.js` * test: add `cli.test.js` * test: update `cli.test.js` to be included in test coverage * chore: fix `pretest` script * refactor: delete unworking try-catch block and replace it with `.on('close', ...)` * test: create test for `index.js` * test: fix wrong import statement in `index.test.js`
- Loading branch information
1 parent
b09baa5
commit 08b195a
Showing
12 changed files
with
145 additions
and
67 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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`); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { clangFormatPath, getClangFormatPath } = require('./index'); | ||
|
||
describe('index ok testing', () => { | ||
it('clangFormatPath should be imported correctly', () => { | ||
ok(clangFormatPath); | ||
}); | ||
it('getClangFormatPath should be imported correctly', () => { | ||
ok(getClangFormatPath); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; |
12 changes: 12 additions & 0 deletions
12
packages/clang-format-node/src/utils/clangFormatPath.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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())); | ||
}); | ||
}); |
35 changes: 35 additions & 0 deletions
35
packages/clang-format-node/src/utils/getClangFormatPath.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters