I have a lot of JavaScript files which look like this:
import {equals} from '../booleans/equality-functions'
import {identity} from '../higher-order-functions'
import {defaultValue} from '../type-functions'
export function fold(f) {
return initialValue => arr => {
let acc = initialValue
for (let i = 0; i < arr.length; i ) {
acc = f(acc, arr[i], i)
}
return acc
}
}
All I want to do is adding .js
to the urls. Meaning I want to convert the import statement import {equals} from '../booleans/equality-functions'
to import {equals} from '../booleans/equality-functions.js'
.
I guess the best way to do it would be a sed
command. Has anybody achieved something similar like this before and could give some advice what kind of regular expression I should use?
Thank you!
CodePudding user response:
With find and sed.
find . -type f -name '*.js' -exec sed -E -i '.bak' "s/(^import.*)(')/\1.j\s\'/g" {}
This will find all imports in all .js
files and add .js to the end before the last quote.
This will also backup files with a .bak
extension.
Change the find as needed.
Note: This is not smart enough to figure out of there already exists a .js
and will end up add a .js
to all imports irrespective.
CodePudding user response:
Using sed
$ sed '/import {[^}]*}/s/.$/.js&/' input_file
import {equals} from '../booleans/equality-functions.js'
import {identity} from '../higher-order-functions.js'
import {defaultValue} from '../type-functions.js'
export function fold(f) {
return initialValue => arr => {
let acc = initialValue
for (let i = 0; i < arr.length; i ) {
acc = f(acc, arr[i], i)
}
return acc
}
}