Skip to content

Commit 86fa1a5

Browse files
committed
add node script hooks
1 parent c297f8a commit 86fa1a5

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

lib/generators/lightning_cad/install_generator.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ def add_javascript_initializers
6262
JS
6363
append_to_file 'app/javascript/application.js', initializer_setup
6464
copy_file 'app/javascript/config/initializers/smartJSON.js', 'app/javascript/config/initializers/smartJSON.js'
65+
copy_file 'app/javascript/helpers/hooks.js', 'app/javascript/helpers/hooks.js'
66+
copy_file 'app/javascript/helpers/register_hooks.js', 'app/javascript/helpers/register_hooks.js'
6567
# To prevent smartJSON from throwing an error
6668
run 'mkdir app/javascript/shared'
6769
run 'mkdir app/javascript/shared/domain-models'
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import path from 'node:path'
2+
import * as glob from 'glob'
3+
4+
const shorthandRoot = path.join(process.cwd(), 'app/javascript')
5+
const config = {
6+
shorthandRoot
7+
}
8+
9+
export function resolve(specifier, context, nextResolve) {
10+
let finalSpecifier = specifier
11+
if (specifier.startsWith('#')) {
12+
finalSpecifier = resolveShorthandPath(finalSpecifier, context, nextResolve)
13+
}
14+
15+
if (specifier.includes('*')) {
16+
return resolveGlobPath(finalSpecifier, context, nextResolve)
17+
}
18+
19+
return nextResolve(finalSpecifier, context)
20+
}
21+
22+
export function load(specifier, context, nextLoad) {
23+
if (!specifier.includes('*')) return nextLoad(specifier, context)
24+
25+
const pattern = specifier.replace('file://', '')
26+
const paths = glob.globSync(pattern, { nodir: true })
27+
28+
const pathParts = pattern.split('/')
29+
const basePathParts = pathParts.slice(0, pathParts.findIndex(part => part.includes('*')))
30+
// we need to determine how many times we have a "*" in a folder level. The imports have to go back that far in order to work properly.
31+
const folderLevels = pathParts.length - basePathParts.length - 1 // minus one for the file
32+
const basePath = basePathParts.join('/')
33+
const baseImportPath = `${Array(folderLevels).fill('../').join('')}`
34+
35+
const getModuleIdentifier = index => `module${index}`
36+
const getImportPath = file => path.join(baseImportPath, path.relative(basePath, file))
37+
const importStatements = paths.map((file, index) => {
38+
return `import * as ${getModuleIdentifier(index)} from './${getImportPath(file)}'`
39+
})
40+
const exportStatement = `export default [${paths.map((_s, index) => getModuleIdentifier(index)).join(', ') }]`
41+
42+
const content = [...importStatements, exportStatement].join('\n')
43+
const source = Buffer.from(content)
44+
45+
return {
46+
format: "module",
47+
source,
48+
shortCircuit: true
49+
}
50+
}
51+
52+
function resolveShorthandPath(specifier, _context, _nextResolve) {
53+
return path.join(config.shorthandRoot, specifier.slice(1))
54+
}
55+
56+
function resolveGlobPath(specifier, context, _nextResolve) {
57+
// We have to manually resolve the path so that we can load all the files that match the pattern later
58+
let finalPath
59+
if (specifier.startsWith('.')) { // Relative path to parent
60+
const basePath = context.parentURL.replace('file://', '').split('/').slice(0, -1).join('/')
61+
finalPath = path.join(basePath, specifier)
62+
} else if (specifier.startsWith('/')) { // Absolute path
63+
finalPath = specifier
64+
} else { // node modules
65+
finalPath = path.join(process.cwd(), 'node_modules', specifier)
66+
}
67+
68+
return {
69+
url: `file://${finalPath}`,
70+
type: "module",
71+
shortCircuit: true
72+
}
73+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { register } from 'node:module'
2+
3+
register('./hooks.js', import.meta.url)

lib/generators/lightning_cad/test_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def add_jasmine_tests
88
say 'Adding jasmine'
99
run "yarn add --dev jasmine@^5.1.0"
1010

11-
run 'npm pkg set scripts.test_shared="NODE_ENV=test NODE_PATH="./node_modules:./app/javascript:$NODE_PATH" jasmine --config=jasmine.json"'
11+
run 'npm pkg set scripts.test_shared="NODE_OPTIONS=\'--import=./app/javascript/helpers/register_hooks.js\' jasmine --config=jasmine.json"'
1212

1313
copy_file 'jasmine.json', 'jasmine.json'
1414
copy_file 'spec/javascript/helpers/initializers.js', 'spec/javascript/helpers/initializers.js'

0 commit comments

Comments
 (0)