mirror of
https://github.com/primer/css.git
synced 2024-12-01 04:21:12 +03:00
42 lines
1.0 KiB
JavaScript
42 lines
1.0 KiB
JavaScript
const {green, red, yellow} = require('colorette')
|
|
const {readFileSync, writeFileSync} = require('fs-extra')
|
|
|
|
const HEADER = '# DO NOT EDIT: automatically generated by ignore.js'
|
|
|
|
module.exports = {getIgnored, setIgnored}
|
|
|
|
function readLines(file) {
|
|
let content
|
|
try {
|
|
content = readFileSync(file, 'utf8')
|
|
} catch (error) {
|
|
console.warn(`ignore file ${file} does not exist!`)
|
|
return []
|
|
}
|
|
|
|
return content.trim().split('\n').map(line => line.trim())
|
|
}
|
|
|
|
function getIgnored(file) {
|
|
const lines = readLines(file)
|
|
const headerIndex = lines.indexOf(HEADER)
|
|
if (headerIndex === -1) {
|
|
console.warn(`ignore file ${file} does not contain the automatically generated header`)
|
|
return []
|
|
}
|
|
|
|
return lines.slice(headerIndex + 1).filter(line => line)
|
|
}
|
|
|
|
function setIgnored(file, files) {
|
|
const lines = readLines(file)
|
|
const headerIndex = lines.indexOf(HEADER)
|
|
if (headerIndex === -1) {
|
|
lines.push(HEADER)
|
|
} else {
|
|
lines.splice(headerIndex + 1)
|
|
}
|
|
lines.push(...files)
|
|
writeFileSync(file, lines.join('\n'), 'utf8')
|
|
}
|