Skip to content

Commit

Permalink
Remove leading ./ when converting filenames since fast-glob includes …
Browse files Browse the repository at this point in the history
…them, but glob did not

Fixes #20
  • Loading branch information
excid3 committed Jun 5, 2023
1 parent 1bcc0f4 commit 3b241a6
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
const path = require('path')
const fg = require('fast-glob')

// Transform filenames to controller names
// [ './admin/hello_world_controller.js', ... ]
// [ 'admin--hello-world', ... ]
function convertFilenameToControllerName(filename) {
return filename
.replace(/^\.\//, "") // Remove ./ prefix
.replace(/_controller.[j|t]s$/, "") // Strip _controller.js extension
.replace(/\//g, "--") // Replace folders with -- namespaces
.replace(/_/g, '-') //
}

// This plugin adds support for globs like "./**/*" to import an entire directory
// We can use this to import arbitrary files or Stimulus controllers and ActionCable channels
const railsPlugin = (options = { matcher: /.+\..+/ }) => ({
Expand All @@ -25,7 +36,6 @@ const railsPlugin = (options = { matcher: /.+\..+/ }) => ({

build.onLoad({ filter: /.*/, namespace: 'rails' }, async (args) => {
// Get a list of all files in the directory
// [ 'accounts_controller.js', ... ]
let files = (
fg.sync(args.pluginData.path, {
cwd: args.pluginData.resolveDir,
Expand All @@ -34,15 +44,7 @@ const railsPlugin = (options = { matcher: /.+\..+/ }) => ({

// Filter to match the import
files = files.sort().filter(path => options.matcher.test(path));

// Transform to controller names
// [ 'accounts', ... ]
const controllerNames = files
.map((module) => module
.replace(/_controller.[j|t]s$/, "")
.replace(/\//g, "--")
.replace(/_/g, '-')
)
const controllerNames = files.map(convertFilenameToControllerName)

const importerCode = `
${files
Expand Down

0 comments on commit 3b241a6

Please sign in to comment.