Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
npx @build-in-blocks/dev.setup@1.0.3 dev:internal:lint
npx @build-in-blocks/dev.setup@1.0.4 dev:internal:lint
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down Expand Up @@ -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
},
````
Expand Down Expand Up @@ -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
# -------------------------------------------------
Expand Down
60 changes: 44 additions & 16 deletions bin/pkg.internal.cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -23,33 +24,41 @@
// 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');

// -----------------------------------------------
// 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...');

Check warning on line 61 in bin/pkg.internal.cli.js

View workflow job for this annotation

GitHub Actions / validate (24.x)

Unexpected console statement

Check warning on line 61 in bin/pkg.internal.cli.js

View workflow job for this annotation

GitHub Actions / validate (23.x)

Unexpected console statement

Check warning on line 61 in bin/pkg.internal.cli.js

View workflow job for this annotation

GitHub Actions / validate (20.x)

Unexpected console statement

Check warning on line 61 in bin/pkg.internal.cli.js

View workflow job for this annotation

GitHub Actions / validate (25.x)

Unexpected console statement

Check warning on line 61 in bin/pkg.internal.cli.js

View workflow job for this annotation

GitHub Actions / validate (22.x)

Unexpected console statement
try {
execSync(`node "${huskyBin}"`, { stdio: 'inherit' });
const preCommitPath = path.join(userAppRoot, '.husky/pre-commit');
Expand All @@ -61,9 +70,28 @@

fs.writeFileSync(preCommitPath, hookContent, { mode: 0o755 });

console.log('✅ Git hooks integrated successfully.');
console.log('[SUCCESS] Husky git hooks integrated successfully.');

Check warning on line 73 in bin/pkg.internal.cli.js

View workflow job for this annotation

GitHub Actions / validate (24.x)

Unexpected console statement

Check warning on line 73 in bin/pkg.internal.cli.js

View workflow job for this annotation

GitHub Actions / validate (23.x)

Unexpected console statement

Check warning on line 73 in bin/pkg.internal.cli.js

View workflow job for this annotation

GitHub Actions / validate (20.x)

Unexpected console statement

Check warning on line 73 in bin/pkg.internal.cli.js

View workflow job for this annotation

GitHub Actions / validate (25.x)

Unexpected console statement

Check warning on line 73 in bin/pkg.internal.cli.js

View workflow job for this annotation

GitHub Actions / validate (22.x)

Unexpected console statement
} 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,
});
}
}

Expand Down
7 changes: 7 additions & 0 deletions config.root/blocks.packages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { blocksTerminalLogger } from '@build-in-blocks/dev.resources';

export {
//-
blocksTerminalLogger,
//-
};
6 changes: 5 additions & 1 deletion config.root/root.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { path, fileURLToPath, createRequire } from './external.packages.js';
import { fs, path, fileURLToPath, createRequire } from './external.packages.js';

// ------------------------------------------------
// ESM & Resolution Helpers:
Expand All @@ -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
Expand All @@ -26,4 +29,5 @@ export {
//-
binPath,
//-
internalPkgJSON,
};
14 changes: 12 additions & 2 deletions package-lock.json

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

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
Expand Down
Loading