Last active
May 27, 2020 09:45
-
-
Save nvgordeev/f607cb3056c45ac365d3c83758b45aa8 to your computer and use it in GitHub Desktop.
This file contains 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
const fs = require('fs'); | |
const readline = require('readline'); | |
const PATH_SRC = './src'; | |
const OUTPUT_PATH = './; | |
const OUTPUT_FILE_NAME = 'snippets.json'; | |
const CONTROL_LINE_PREFIX = '// #'; | |
function processFile(fileName, resolve, reject) { | |
const reader = readline.createInterface({ | |
input: fs.createReadStream(`${PATH_SRC}/${fileName}`), | |
}); | |
const controlLines = []; | |
const body = []; | |
reader.on('line', (line) => { | |
if (line.startsWith(CONTROL_LINE_PREFIX)) { | |
const clearedLine = line.replace(CONTROL_LINE_PREFIX, '').replace(/^\s\s*/, '').replace(/\s\s*$/, ''); | |
controlLines.push(clearedLine); | |
} else { | |
body.push(line); | |
} | |
}); | |
reader.on('close', () => { | |
const [name, prefix, description] = controlLines; | |
resolve({ [name]: { prefix, body, description } }); | |
}); | |
reader.on('error', e => reject(e)); | |
} | |
function promisedFileProcess(fileName) { | |
return new Promise((resolve, reject) => processFile(fileName, resolve, reject)); | |
} | |
function processFiles(fileNames) { | |
return Promise.all(fileNames.map(promisedFileProcess)); | |
} | |
function prepareResult(arr) { | |
return arr.reduce( | |
(res, curr) => { | |
const key = Object.keys(curr)[0]; | |
return { ...res, [key]: curr[key] }; | |
}, | |
{ | |
scope: 'javascript, typescript', | |
}, | |
); | |
} | |
function storeResult(result) { | |
fs.writeFile( | |
`${OUTPUT_PATH}/${OUTPUT_FILE_NAME}`, | |
JSON.stringify(result, null, 2), | |
(err) => { | |
if (err) { | |
return err; | |
} | |
console.log('all done'); | |
}, | |
); | |
} | |
function processError(e) { | |
console.error('Something goes wrong', e); | |
} | |
(function main() { | |
fs.readdir(PATH_SRC, (err, fileNames) => { | |
processFiles(fileNames) | |
.then(prepareResult) | |
.then(storeResult) | |
.catch(processError); | |
}); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Snippet creator
Что делает
Создает сниппеты для
VSCode
из обычных файлов.Зачем
Как сделать исходник
// #
, это будут название, шорткат и описание сниппета соответственно. Например:// # Jest screenshot test with context
// # autoru-jest-screenshot-context
// # Скриншотный тест компонента с контекстом
Пример исходного файла:
Как превратить это все в сниппеты
PATH_SRC
OUTPUT_PATH