Last active
September 28, 2022 09:00
-
-
Save jmfrancois/402c32c22fba98f1e35599f1e0dab2c2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const fs = require('fs'); | |
const path = require('path'); | |
const CWD = process.cwd(); | |
const EXTENSIONS = /\.(scss)$/; | |
const SASS_DATA = "@use '~@talend/bootstrap-theme/src/theme/guidelines' as *;\n"; | |
function transform(filename) { | |
console.log(filename); | |
const fileContent = fs.readFileSync(filename).toString(); | |
const found = fileContent.includes(SASS_DATA); | |
if (found) { | |
console.log(filename, 'SASS_DATA var found'); | |
return; | |
} | |
console.log(filename, 'add SASS_DATA var'); | |
fs.writeFileSync(filename, [SASS_DATA, fileContent].join('\n')); | |
} | |
function findAllSrcFiles( | |
current = path.join(CWD, process.argv[process.argv.length - 1]), | |
buff = [], | |
) { | |
return fs.readdirSync(current, { withFileTypes: true }).reduce((acc, info) => { | |
if (info.isDirectory()) { | |
return acc.concat(findAllSrcFiles(path.join(current, info.name))); | |
} | |
if (info.name.match(EXTENSIONS)) { | |
acc.push(path.join(current, info.name)); | |
} | |
return acc; | |
}, buff); | |
} | |
const result = findAllSrcFiles(); | |
result.forEach(filename => { | |
transform(filename); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const fs = require('fs'); | |
const path = require('path'); | |
const CWD = process.cwd(); | |
const scssRE = /import.*from\ \'(\..*\.scss)\'\;/; | |
function transform(filename) { | |
console.log(filename); | |
const fileContent = fs.readFileSync(filename).toString(); | |
if (!fileContent.includes('.scss')) { | |
console.log(filename, 'scss module not found'); | |
return; | |
} | |
console.log(filename, 'scss module found !'); | |
const newLines = fileContent.split('\n').map(line => { | |
const match = line.match(scssRE); | |
if (match === null) { | |
return line; | |
} | |
if (line.includes('.module.')) { | |
return line; | |
} | |
const scssFileName = match[1]; | |
const scssFilePath = path.join(path.dirname(filename), scssFileName); | |
if (fs.existsSync(scssFilePath)) { | |
fs.renameSync(scssFilePath, scssFilePath.replace('.scss', '.module.scss')); | |
} | |
return line.replace('.scss', '.module.scss'); | |
}); | |
//TODO rename this file | |
fs.writeFileSync(filename, newLines.join('\n')); | |
} | |
const EXTENSIONS = /\.(js|ts|tsx)$/; | |
function findAllSrcFiles( | |
current = path.join(CWD, process.argv[process.argv.length - 1]), | |
buff = [], | |
) { | |
return fs.readdirSync(current, { withFileTypes: true }).reduce((acc, info) => { | |
if (info.isDirectory()) { | |
return acc.concat(findAllSrcFiles(path.join(current, info.name))); | |
} | |
if (info.name.match(EXTENSIONS)) { | |
acc.push(path.join(current, info.name)); | |
} | |
return acc; | |
}, buff); | |
} | |
const result = findAllSrcFiles(); | |
result.forEach(filename => { | |
transform(filename); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment