diff --git a/.husky/pre-commit b/.husky/pre-commit index 05ab555..e5f6132 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1 +1 @@ -npx @build-in-blocks/dev.setup@1.0.3 dev:internal:lint \ No newline at end of file +npx @build-in-blocks/dev.setup@1.0.4 dev:internal:lint \ No newline at end of file diff --git a/README.md b/README.md index 6053a00..7d6cc99 100644 --- a/README.md +++ b/README.md @@ -41,11 +41,17 @@ #### 1. Main package installation -Install our dev setup package as a `devDependency` in your project: +- Install our dev setup package as a `devDependency` in your project: -```` -npm install -D @build-in-blocks/dev.setup -```` + ```` + npm install -D @build-in-blocks/dev.setup --save-exact + ```` + +- Also make sure to install the resources package, so that your project's code can compile successfully without errors: + + ```` + npm install -D @build-in-blocks/dev.resources --save-exact + ```` #### 2. When to install `typescript` @@ -105,7 +111,7 @@ export default { ```` "scripts": { - "prepare": "npx @build-in-blocks/dev.setup@1.0.3 dev:husky:setup:git" + "prepare": "npx @build-in-blocks/dev.setup@1.0.4 dev:husky:setup:git" // your other npm scripts in your project goes here as usual }, ```` @@ -200,7 +206,7 @@ export default { # ----------------------------------------------------------------- # This points to the shared library repository's "central" workflow # ----------------------------------------------------------------- - uses: build-in-blocks/dev.setup/.github/workflows/central-blocks-ci.yml@v1.0.3 + uses: build-in-blocks/dev.setup/.github/workflows/central-blocks-ci.yml@v1.0.4 with: run_tests: true # ------------------------------------------------- diff --git a/bin/pkg.internal.cli.js b/bin/pkg.internal.cli.js index 34b1caa..b8b189d 100755 --- a/bin/pkg.internal.cli.js +++ b/bin/pkg.internal.cli.js @@ -3,9 +3,10 @@ /*global console*/ import { fs, path, execSync, process } from '../config.root/external.packages.js'; -import { binPath, __dirname } from '../config.root/root.js'; +import { blocksTerminalLogger } from '../config.root/blocks.packages.js'; +import { internalPkgJSON, __dirname } from '../config.root/root.js'; -const internalCommand = '@build-in-blocks/dev.setup@1.0.3'; +const internalCommand = '@build-in-blocks/dev.setup@1.0.4'; const userAppArg = { huskyGitSetup: 'dev:husky:setup:git', @@ -23,6 +24,8 @@ if (pkgArgDetected) { // this library, or the web app that this library is used in). //----------------------------------------------------------------- const userAppRoot = process.cwd(); + const userAppPkgJSON = JSON.parse(fs.readFileSync(path.join(userAppRoot, 'package.json'), 'utf-8')); + //- const engineRoot = path.resolve(__dirname, '..'); const internalModulesPath = path.resolve(engineRoot, 'node_modules'); const internalBinPath = path.resolve(internalModulesPath, '.bin'); @@ -30,26 +33,32 @@ if (pkgArgDetected) { // ----------------------------------------------- // Safely find binary paths for the internal tools // ----------------------------------------------- - const getBinPath = ({ pkgName, binSubPath }) => { - try { - return binPath({ pkgName, binSubPath }); - } catch { - return path.resolve(internalBinPath, pkgName); + const getBinPath = (pkgName) => { + //----------------------------------------------------------------- + // A. Look in the User App's .bin (Production or hoisted) + // B. Look in the Engine's .bin (Development i.e. local npm link) + // C. Return the .bin path that match based on environment detected + //----------------------------------------------------------------- + const paths = [path.resolve(userAppRoot, 'node_modules/.bin', pkgName), path.resolve(engineRoot, 'node_modules/.bin', pkgName)]; + + const found = paths.find((p) => fs.existsSync(p)); + + if (!found) { + throw new Error(`Binary for ${pkgName} not found. Try 'npm install'`); } + + return found; }; - //- - const huskyBin = getBinPath({ pkgName: 'husky', binSubPath: 'bin.js' }); - const lintStagedBin = getBinPath({ - pkgName: 'lint-staged', - binSubPath: 'bin/lint-staged.js', - }); + + const huskyBin = getBinPath('husky'); + const lintStagedBin = getBinPath('lint-staged'); // --------------------------------------------------------- // Set up husky to work with git i.e. initially generate the // husky folder, with content that includes pre-commit file. // --------------------------------------------------------- if (command === userAppArg.huskyGitSetup) { - console.log('🐶 Setting up @build-in-blocks git hooks...'); + console.log('[PREPARING] Setting up husky git hooks...'); try { execSync(`node "${huskyBin}"`, { stdio: 'inherit' }); const preCommitPath = path.join(userAppRoot, '.husky/pre-commit'); @@ -61,9 +70,28 @@ if (pkgArgDetected) { fs.writeFileSync(preCommitPath, hookContent, { mode: 0o755 }); - console.log('✅ Git hooks integrated successfully.'); + console.log('[SUCCESS] Husky git hooks integrated successfully.'); } catch { - console.error('❌ Git hook setup failed.'); + blocksTerminalLogger({ + startLoggerMessageOnNewLine: true, + internalPackage: { + fullName: internalPkgJSON.name, + }, + userApp: { + fullName: userAppPkgJSON.name, + errorMessage: 'Husky git hook setup failed.', + }, + errorSource: true, + suggestion: { + // prettier-ignore + messageList: [ + '→ [Step 1] Run "git init" at the root of your project to make it a git repository.', + '→ [Step 2] Add and commit one or more files to git.', + '→ [Step 3] Try the prepare script again, after you\'ve done steps 1 & 2 above.', + ], + }, + processExit: true, + }); } } diff --git a/config.root/blocks.packages.js b/config.root/blocks.packages.js new file mode 100644 index 0000000..e838de8 --- /dev/null +++ b/config.root/blocks.packages.js @@ -0,0 +1,7 @@ +import { blocksTerminalLogger } from '@build-in-blocks/dev.resources'; + +export { + //- + blocksTerminalLogger, + //- +}; diff --git a/config.root/root.js b/config.root/root.js index eb388a8..b3d4dbb 100644 --- a/config.root/root.js +++ b/config.root/root.js @@ -1,4 +1,4 @@ -import { path, fileURLToPath, createRequire } from './external.packages.js'; +import { fs, path, fileURLToPath, createRequire } from './external.packages.js'; // ------------------------------------------------ // ESM & Resolution Helpers: @@ -16,6 +16,9 @@ const binPath = ({ pkgName, binSubPath }) => { return path.join(_pkgRoot, binSubPath); }; +const internalProjectRoot = path.join(__dirname, '../', 'package.json'); +const internalPkgJSON = JSON.parse(fs.readFileSync(internalProjectRoot, 'utf-8')); + export { //------------------------------- // Export global access variables @@ -26,4 +29,5 @@ export { //- binPath, //- + internalPkgJSON, }; diff --git a/package-lock.json b/package-lock.json index 477d173..cf30998 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@build-in-blocks/dev.setup", - "version": "1.0.3", + "version": "1.0.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@build-in-blocks/dev.setup", - "version": "1.0.3", + "version": "1.0.4", "license": "AGPL-3.0", "dependencies": { "@eslint/js": "^10.0.1", @@ -20,8 +20,18 @@ "bin": { "blocks.pkg.dev.setup": "bin/pkg.internal.cli.js", "dev.setup": "bin/pkg.internal.cli.js" + }, + "devDependencies": { + "@build-in-blocks/dev.resources": "1.0.4" } }, + "node_modules/@build-in-blocks/dev.resources": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/@build-in-blocks/dev.resources/-/dev.resources-1.0.4.tgz", + "integrity": "sha512-6Xn1/+mU3LIGNcvYH8uM4goqVl6/EeWgDrwj87NkV8alOnn4MM4D1SeahQBbHSiFoaxDAWScKMVr2+1oXFzQxw==", + "dev": true, + "license": "AGPL-3.0" + }, "node_modules/@eslint-community/eslint-utils": { "version": "4.9.1", "resolved": "https://registry.npmjs.org/@eslint-community/eslint-utils/-/eslint-utils-4.9.1.tgz", diff --git a/package.json b/package.json index 2a0e780..50f46d9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@build-in-blocks/dev.setup", - "version": "1.0.3", + "version": "1.0.4", "description": "Code linting, formatting, pre-commit hook and GitHub Actions Continuous Integration (CI) (including node version compatiblity check) development environment setup for your typescript code repository", "type": "module", "main": "config.programmed/eslint.helper.mjs", @@ -29,6 +29,9 @@ "prettier": "^3.8.1", "typescript-eslint": "^8.56.1" }, + "devDependencies": { + "@build-in-blocks/dev.resources": "1.0.4" + }, "homepage": "https://github.com/build-in-blocks/dev.setup#readme", "bugs": { "url": "https://github.com/build-in-blocks/product-issue-reports/issues"