Skip to content

Commit

Permalink
ci(typescript): transformer (#8020)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaMan123 authored Jun 25, 2022
1 parent 92dda74 commit 351b9e8
Show file tree
Hide file tree
Showing 4 changed files with 501 additions and 13 deletions.
83 changes: 83 additions & 0 deletions scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,38 @@ const Checkbox = require('inquirer-checkbox-plus-prompt');
const commander = require('commander');
const program = new commander.Command();

const { transform: transformFiles, listFiles } = require('./transform_files');

const CLI_CACHE = path.resolve(__dirname, 'cli_cache.json');
const wd = path.resolve(__dirname, '..');
const websiteDir = path.resolve(wd, '../fabricjs.com');

function execGitCommand(cmd) {
return cp.execSync(cmd, { cwd: wd }).toString()
.replace(/\n/g, ',')
.split(',')
.map(value => value.trim())
.filter(value => value.length > 0);
}

function getGitInfo(branchRef) {
const branch = execGitCommand('git branch --show-current')[0];
const tag = execGitCommand('git describe --tags')[0];
const uncommittedChanges = execGitCommand('git status --porcelain').map(value => {
const [type, path] = value.split(' ');
return { type, path };
});
const changes = execGitCommand(`git diff ${branchRef} --name-only`);
const userName = execGitCommand('git config user.name')[0];
return {
branch,
tag,
uncommittedChanges,
changes,
user: userName
};
}

class ICheckbox extends Checkbox {
constructor(questions, rl, answers) {
super(questions, rl, answers);
Expand Down Expand Up @@ -223,6 +251,31 @@ function createChoiceData(type, file) {
}
}

async function selectFileToTransform() {
const files = _.map(listFiles(), ({ dir, file }) => createChoiceData(path.relative(path.resolve(wd,'src'), dir).replaceAll('\\','/'), file));
const { tests: filteredTests } = await inquirer.prompt([
{
type: 'test-selection',
name: 'tests',
message: 'Select files to transform to es6',
highlight: true,
searchable: true,
default: [],
pageSize: 10,
source(answersSoFar, input = '') {
return new Promise(resolve => {
const value = _.map(this.getCurrentValue(), value => createChoiceData(value.type, value.file));
const res = fuzzy.filter(input, files, {
extract: (item) => item.name
}).map((element) => element.original);
resolve(value.concat(_.differenceBy(res, value, 'name')));
});
}
}
]);
return filteredTests.map(({ type, file }) => path.resolve(wd, 'src', type, file));
}

async function selectTestFile() {
const selected = readCLIFile();
const unitTests = listTestFiles('unit').map(file => createChoiceData('unit', file));
Expand Down Expand Up @@ -363,4 +416,34 @@ website
.option('-w, --watch')
.action(exportToWebsite);

program
.command('transform')
.description('transforms files into es6')
.option('-o, --overwrite', 'overwrite exisitng files', false)
.option('-x, --no-exports', 'do not use exports')
.option('-i, --index', 'create index files', false)
.option('-ts, --typescript', 'transform into typescript', false)
.option('-v, --verbose', 'verbose logging', true)
.option('-a, --all', 'transform all files', false)
.option('-d, --diff [branch]', 'compare against given branch (default: master) and transform all files with diff')
.action(async ({ overwrite, exports, index, typescript, verbose, all, diff: gitRef } = {}) => {
let files = [];
if (gitRef) {
gitRef = gitRef === true ? 'master' : gitRef;
const { changes } = getGitInfo(gitRef);
files = changes.map(change => path.resolve(wd, change));
}
else if (!all) {
files = await selectFileToTransform();
}
transformFiles({
overwriteExisitingFiles: overwrite,
useExports: exports,
createIndex: index,
ext: typescript ? 'ts' : 'js',
verbose,
files
});
});

program.parse(process.argv);
Loading

0 comments on commit 351b9e8

Please sign in to comment.