aberlaas-helper 2.18.1 → 2.20.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/helper.js ADDED
@@ -0,0 +1,195 @@
1
+ import path from 'node:path';
2
+ import { _ } from 'golgoth';
3
+ import {
4
+ env,
5
+ exists,
6
+ firostError,
7
+ firostImport,
8
+ run as firostRun,
9
+ gitRoot,
10
+ glob,
11
+ packageRoot,
12
+ wrap,
13
+ } from 'firost';
14
+
15
+ // Exported wrapper object so we can mock indidividual methods in tests by
16
+ // spying on __ like this:
17
+ // import { __, methodName } from
18
+ // vi.spyOn(__, 'methodName').mockReturnValue()
19
+ export const __ = {
20
+ /**
21
+ * Absolute path of where the user was when running the initial "yarn run"
22
+ * command that triggered aberlaas
23
+ * @returns {string} Absolute path to working directory
24
+ */
25
+ hostWorkingDirectory() {
26
+ // INIT_CWD is set by yarn as the directory where the yarn command is being
27
+ // called
28
+ return env('INIT_CWD') || process.cwd();
29
+ },
30
+
31
+ /**
32
+ * Absolute path to the closest package root
33
+ * @returns {string} Absolute path to closest package root
34
+ */
35
+ hostPackageRoot() {
36
+ return packageRoot(__.hostWorkingDirectory());
37
+ },
38
+
39
+ /**
40
+ * Return an absolute path to a file in the host package folder
41
+ * @param {string} relativePath Relative path from the host package root
42
+ * @returns {string} Absolute path to the host file
43
+ */
44
+ hostPackagePath(relativePath = '') {
45
+ return path.resolve(__.hostPackageRoot(), relativePath);
46
+ },
47
+
48
+ /**
49
+ * Find files in host package directory following glob patterns.
50
+ * Will exclude some directories by default, and allow specifying only
51
+ * specific file extensions
52
+ * @param {Array} userPattern Patterns to match
53
+ * @param {Array} safeExtensions Optional array of extensions to safelist. If
54
+ * set, only files of this extensions will be returned
55
+ * @returns {Array} Array of files matching the patterns
56
+ */
57
+ async findHostPackageFiles(userPattern, safeExtensions = []) {
58
+ const patterns = [
59
+ ..._.castArray(userPattern),
60
+ // Exclude folders that shouldn't be included
61
+ '!**/build/**',
62
+ '!**/dist/**',
63
+ '!**/fixtures/**',
64
+ '!**/node_modules/**',
65
+ '!**/tmp/**',
66
+ '!**/vendors/**',
67
+ '!**/.claude/**',
68
+ '!**/.git/**',
69
+ '!**/.next/**',
70
+ '!**/.turbo/**',
71
+ '!**/.yarn/**',
72
+ ];
73
+
74
+ // Expanding globs
75
+ let allFiles = await glob(patterns, {
76
+ directories: false,
77
+ cwd: __.hostPackageRoot(),
78
+ });
79
+
80
+ if (_.isEmpty(safeExtensions)) {
81
+ return allFiles;
82
+ }
83
+
84
+ // Keep only files of specified extensions
85
+ allFiles = _.filter(allFiles, (filepath) => {
86
+ const extension = path.extname(filepath);
87
+ const extensionWithoutDot = _.replace(extension, '.', '');
88
+ return (
89
+ _.includes(safeExtensions, extension) ||
90
+ _.includes(safeExtensions, extensionWithoutDot)
91
+ );
92
+ });
93
+
94
+ return allFiles;
95
+ },
96
+
97
+ /**
98
+ * Return absolute path to the host dir
99
+ * @returns {string} Absolute path to host dir
100
+ */
101
+ hostGitRoot() {
102
+ return gitRoot(__.hostWorkingDirectory());
103
+ },
104
+
105
+ /**
106
+ * Return an absolute path to a file in the host
107
+ * @param {string} relativePath Relative path from the host root
108
+ * @returns {string} Absolute path to the host file
109
+ */
110
+ hostGitPath(relativePath = '') {
111
+ return path.resolve(__.hostGitRoot(), relativePath);
112
+ },
113
+
114
+ /**
115
+ * Return a config object for a specific tool.
116
+ * Will first check for the user supplied path to the config file, then
117
+ * fallback to the default config file in the host, and finally fallback to
118
+ * the default base config in the aberlaas module.
119
+ * @param {string} userConfigPath User specified config file, relative to the host root
120
+ * @param {string} hostConfigPath Default host config path, relative to the host root
121
+ * @param {object} baseConfig Base aberlaas config, final fallback
122
+ * @returns {object} Config object
123
+ */
124
+ async getConfig(userConfigPath, hostConfigPath, baseConfig = {}) {
125
+ // Taking value from --config in CLI in priority
126
+ if (userConfigPath) {
127
+ const configPath = __.hostGitPath(userConfigPath);
128
+ if (!(await exists(configPath))) {
129
+ throw firostError(
130
+ 'ABERLAAS_HELPER_GET_CONFIG_USER_PROVIDED_NOT_FOUND',
131
+ `Provided config file (${userConfigPath}) not found`,
132
+ );
133
+ }
134
+ // TODO: Add a test for that
135
+
136
+ return await firostImport(configPath, { forceReload: true });
137
+ }
138
+
139
+ // Checking for custom config in the host
140
+ if (hostConfigPath) {
141
+ const hostConfigFullPath = __.hostGitPath(hostConfigPath);
142
+ if (await exists(hostConfigFullPath)) {
143
+ return await firostImport(hostConfigFullPath, { forceReload: true });
144
+ }
145
+ }
146
+
147
+ // Fallback on default config in aberlaas
148
+ return baseConfig;
149
+ },
150
+
151
+ /**
152
+ * Debug command, prints useful info about the host environment
153
+ * Used in tests, to double check in real conditions, what the various paths
154
+ * refer to
155
+ */
156
+ /**
157
+ * Run a yarn command from the host git root
158
+ * @param {string} command Command to run (e.g., 'test', 'lint', 'test --failFast')
159
+ * @returns {Promise} Result of the command execution
160
+ */
161
+ async yarnRun(command) {
162
+ const cwd = __.hostGitRoot();
163
+ return await firostRun(`yarn run ${command}`, {
164
+ cwd,
165
+ env: { FORCE_COLOR: '1' },
166
+ });
167
+ },
168
+
169
+ async run() {
170
+ console.log(
171
+ JSON.stringify(
172
+ {
173
+ hostWorkingDirectory: __.hostWorkingDirectory(),
174
+ hostPackageRoot: __.hostPackageRoot(),
175
+ hostGitRoot: __.hostGitRoot(),
176
+ },
177
+ null,
178
+ 2,
179
+ ),
180
+ );
181
+ },
182
+ };
183
+
184
+ // Named exports of each method, but they wrap a call to __.{methodName}.
185
+ // This allow us to mock __.{methodName} in tests, and still having the named
186
+ // export use the mocked versions of the inner methods.
187
+ export const hostWorkingDirectory = wrap(__, 'hostWorkingDirectory');
188
+ export const hostPackageRoot = wrap(__, 'hostPackageRoot');
189
+ export const hostPackagePath = wrap(__, 'hostPackagePath');
190
+ export const findHostPackageFiles = wrap(__, 'findHostPackageFiles');
191
+ export const hostGitRoot = wrap(__, 'hostGitRoot');
192
+ export const hostGitPath = wrap(__, 'hostGitPath');
193
+ export const getConfig = wrap(__, 'getConfig');
194
+ export const yarnRun = wrap(__, 'yarnRun');
195
+ export const run = wrap(__, 'run');
package/lib/main.js CHANGED
@@ -1,100 +1,14 @@
1
- import path from 'node:path';
2
- import { _ } from 'golgoth';
3
- import { exists, firostImport, glob } from 'firost';
4
-
5
- export default {
6
- /**
7
- * Return absolute path to the host dir
8
- * @returns {string} Absolute path to host dir
9
- */
10
- hostRoot() {
11
- return process.cwd();
12
- },
13
- /**
14
- * Return an absolute path to a file in the host
15
- * @param {string} relativePath Relative path from the host root
16
- * @returns {string} Absolute path to the host file
17
- */
18
- hostPath(relativePath = '') {
19
- return path.resolve(this.hostRoot(), relativePath);
20
- },
21
- /**
22
- * Find files in host directory following glob patterns. Will exclude some
23
- * directories by default, and allow specifying only specific file extensions
24
- * @param {Array} userPattern Patterns to match
25
- * @param {Array} safeExtensions Optional array of extensions to safelist. If
26
- * set, only files of this extensions will be returned
27
- * @returns {Array} Array of files matching the patterns
28
- */
29
- async findHostFiles(userPattern, safeExtensions = []) {
30
- const patterns = _.castArray(userPattern);
31
- // Making all path relative to the host
32
- const globs = _.map(patterns, (pattern) => {
33
- return this.hostPath(pattern);
34
- });
35
-
36
- // Exclude folders that shouldn't be included
37
- const blockedFolders = [
38
- 'build',
39
- 'dist',
40
- 'fixtures',
41
- 'node_modules',
42
- 'tmp',
43
- 'vendors',
44
- '.git',
45
- '.yarn',
46
- '.claude',
47
- '.next',
48
- ];
49
- _.each(blockedFolders, (blockedFolder) => {
50
- const deepFolder = `**/${blockedFolder}/**`;
51
- globs.push(`!${this.hostPath(deepFolder)}`);
52
- });
53
-
54
- // Expanding globs
55
- let allFiles = await glob(globs, { directories: false });
56
-
57
- if (_.isEmpty(safeExtensions)) {
58
- return allFiles;
59
- }
60
-
61
- // Keep only files of specified extensions
62
- allFiles = _.filter(allFiles, (filepath) => {
63
- const extension = path.extname(filepath);
64
- const extensionWithoutDot = _.replace(extension, '.', '');
65
- return (
66
- _.includes(safeExtensions, extension) ||
67
- _.includes(safeExtensions, extensionWithoutDot)
68
- );
69
- });
70
-
71
- return allFiles;
72
- },
73
- /**
74
- * Return a config object for a specific tool.
75
- * Will first check for the user supplied path to the config file, then
76
- * fallback to the default config file in the host, and finally fallback to
77
- * the default base config in the aberlaas module.
78
- * @param {string} userConfigPath User specified config file, relative to the host root
79
- * @param {string} hostConfigPath Default host config path, relative to the host root
80
- * @param {object} baseConfig Base aberlaas config, final fallback
81
- * @returns {object} Config object
82
- */
83
- async getConfig(userConfigPath, hostConfigPath, baseConfig = {}) {
84
- // Taking value from --config in CLI in priority
85
- if (userConfigPath) {
86
- return await firostImport(this.hostPath(userConfigPath));
87
- }
88
-
89
- // Checking for custom config in the host
90
- if (hostConfigPath) {
91
- const hostConfigFullPath = this.hostPath(hostConfigPath);
92
- if (await exists(hostConfigFullPath)) {
93
- return await firostImport(hostConfigFullPath);
94
- }
95
- }
96
-
97
- // Fallback on default config in aberlaas
98
- return baseConfig;
99
- },
100
- };
1
+ // Helper
2
+ export { findHostPackageFiles } from './helper.js';
3
+ export { getConfig } from './helper.js';
4
+ export { hostGitPath } from './helper.js';
5
+ export { hostGitRoot } from './helper.js';
6
+ export { hostPackagePath } from './helper.js';
7
+ export { hostPackageRoot } from './helper.js';
8
+ export { hostWorkingDirectory } from './helper.js';
9
+ export { run } from './helper.js';
10
+ export { yarnRun } from './helper.js';
11
+ export { __ } from './helper.js';
12
+
13
+ // Test helper
14
+ export { mockHelperPaths } from './test-helper.js';
@@ -0,0 +1,18 @@
1
+ import { __ as helper } from './helper.js';
2
+ /**
3
+ * Safely mock aberlaas helper functions to point to a test directory.
4
+ * This prevents tests from accidentally modifying the real aberlaas repository.
5
+ * All helper path functions (hostGitRoot, hostPackageRoot, hostWorkingDirectory)
6
+ * will point to the same test directory as a safety measure.
7
+ * You can override specific paths afterwards if needed.
8
+ * @param {string} testDirectory - Absolute path to test directory
9
+ */
10
+ export function mockHelperPaths(testDirectory) {
11
+ /* eslint-disable no-undef */
12
+ vi.spyOn(helper, 'hostGitRoot').mockReturnValue(testDirectory);
13
+ vi.spyOn(helper, 'hostPackageRoot').mockReturnValue(`${testDirectory}/lib`);
14
+ vi.spyOn(helper, 'hostWorkingDirectory').mockReturnValue(
15
+ `${testDirectory}/lib/src`,
16
+ );
17
+ /* eslint-enable no-undef */
18
+ }
package/package.json CHANGED
@@ -1,14 +1,17 @@
1
1
  {
2
2
  "name": "aberlaas-helper",
3
3
  "type": "module",
4
+ "sideEffects": false,
4
5
  "description": "aberlaas shared helper: Common methods used by all commands",
5
- "version": "2.18.1",
6
+ "version": "2.20.1",
6
7
  "repository": "pixelastic/aberlaas",
7
8
  "homepage": "https://projects.pixelastic.com/aberlaas/",
8
9
  "author": "Tim Carry (@pixelastic)",
9
10
  "license": "MIT",
10
11
  "files": [
11
- "lib/*.js"
12
+ "lib/main.js",
13
+ "lib/helper.js",
14
+ "lib/test-helper.js"
12
15
  ],
13
16
  "exports": {
14
17
  ".": "./lib/main.js"
@@ -17,25 +20,22 @@
17
20
  "engines": {
18
21
  "node": ">=18.18.0"
19
22
  },
20
- "scripts": {
21
- "build": "../../scripts/local/build",
22
- "build:prod": "../../scripts/local/build-prod",
23
- "cms": "../../scripts/local/cms",
24
- "serve": "../../scripts/local/serve",
25
- "ci": "../../scripts/local/ci",
26
- "release": "../../scripts/local/release",
27
- "update-dependencies": "node ../../scripts/meta/update-dependencies.js",
28
- "test:meta": "../../scripts/local/test-meta",
29
- "test": "../../scripts/local/test",
30
- "test:watch": "../../scripts/local/test-watch",
31
- "compress": "../../scripts/local/compress",
32
- "lint": "../../scripts/local/lint",
33
- "lint:fix": "../../scripts/local/lint-fix"
34
- },
35
23
  "dependencies": {
36
- "find-up": "7.0.0",
37
- "firost": "5.2.1",
24
+ "aberlaas-versions": "workspace:*",
25
+ "firost": "5.5.1",
38
26
  "golgoth": "3.0.0"
39
27
  },
40
- "gitHead": "00b60dcf9aebab442a809ccc81942005d87d1b5c"
28
+ "scripts": {
29
+ "build": "cd ../docs && yarn run build",
30
+ "build:prod": "cd ../docs && yarn run build:prod",
31
+ "cms": "cd ../docs && yarn run cms",
32
+ "serve": "cd ../docs && yarn run serve",
33
+ "release": "cd ../.. && ./scripts/release",
34
+ "test:meta": "cd ../.. && ./scripts/test-meta",
35
+ "test": "cd ../.. && ./scripts/test",
36
+ "test:watch": "cd ../.. && ./scripts/test-watch",
37
+ "compress": "cd ../.. && ./scripts/compress",
38
+ "lint": "cd ../.. && ./scripts/lint",
39
+ "lint:fix": "cd ../.. && ./scripts/lint-fix"
40
+ }
41
41
  }