vue-declassify
Version:
Convert Vue 2 class-based components into object-based syntax.
51 lines (50 loc) • 1.8 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const commander_1 = __importDefault(require("commander"));
const ts_morph_1 = require("ts-morph");
const index_1 = require("./index");
commander_1.default
.name('vue-declassify')
.version('1.0.0')
.command('declassify <component>', {
isDefault: true,
})
.description('rewrites the TypeScript Vue component to object-based syntax', {
component: 'path to the component (.ts or .vue file)',
})
.action((path) => {
const project = new ts_morph_1.Project({
skipAddingFilesFromTsConfig: true,
skipFileDependencyResolution: true,
skipLoadingLibFiles: true,
useInMemoryFileSystem: true,
// TODO: pass these options as CLI arguments.
manipulationSettings: {
newLineKind: ts_morph_1.NewLineKind.LineFeed,
quoteKind: ts_morph_1.QuoteKind.Single,
useTrailingCommas: false,
indentationText: ts_morph_1.IndentationText.TwoSpaces,
},
});
let mode;
if (path.endsWith('.vue')) {
mode = 'vue';
}
else if (path.endsWith('.ts')) {
mode = 'ts';
}
else {
throw new Error('Path doesn\'t seem to be a .ts or .vue file.');
}
// TODO: pass the encoding as a CLI argument.
const rwOptions = { encoding: 'utf-8' };
const code = fs_1.default.readFileSync(path, rwOptions);
fs_1.default.writeFileSync(path, index_1.declassify(project, code, mode), rwOptions);
console.log(`Successfully declassified ${path}!`);
})
.parse(process.argv);