Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Commit

Permalink
[WIP] fix body update
Browse files Browse the repository at this point in the history
  • Loading branch information
mubaidr committed Dec 16, 2019
1 parent 798dd6f commit 7087ecf
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 21 deletions.
3 changes: 2 additions & 1 deletion __tests__/lib/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ describe('vue2-migration-helper', () => {
test('should be able to extract js ast from vue sfc', () => {
expect(
vue2MigrationHelper({
path: './__tests__/data/text.vue'
source: './__tests__/data/text.vue',
dryRun: true
})
).toBeDefined()
})
Expand Down
26 changes: 21 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
"@types/eslint": "^6.1.3",
"@types/eslint-plugin-prettier": "^2.2.0",
"@types/gh-pages": "^2.0.1",
"@types/glob": "^7.1.1",
"@types/jest": "^24.0.23",
"@types/prettier": "^1.19.0",
"@types/yargs": "^13.0.3",
"@typescript-eslint/eslint-plugin": "^2.11.0",
"@typescript-eslint/parser": "^2.11.0",
"babel-eslint": "^10.0.3",
Expand All @@ -41,7 +43,7 @@
"eslint-config-prettier": "^6.7.0",
"eslint-plugin-import": "^2.19.1",
"eslint-plugin-jest": "^23.1.1",
"eslint-plugin-prettier": "^3.1.1",
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-vue": "^6.0.1",
"gh-pages": "^2.1.1",
"jest": "^24.9.0",
Expand Down
32 changes: 21 additions & 11 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
#!/usr/bin/env node
import { green, red } from 'chalk'
import { green, red, yellow } from 'chalk'
import glob from 'glob'
import { resolve } from 'path'
import { scriptName } from 'yargs'
// const fs = require('fs')
import { vue2MigrationHelper } from '../index'

const options = scriptName('vue2-migration-helper')
.option('source', {
alias: ['s'],
describe: 'Source directory containing Vue single file components.',
demandOption: true
demandOption: true,
type: 'string'
})
.option('target', {
alias: ['t'],
describe: 'Target directory to save transformed components.',
demandOption: false
demandOption: false,
type: 'string'
})
.option('dry-run', {
alias: ['d', 'dryRun'],
describe: 'Target directory to save transformed components.',
demandOption: false,
default: false
default: false,
type: 'boolean'
})
.help()
.example('vue2-migration-helper --dry-run --s="source" --t="target"').argv
.example('vue2-migration-helper --dry-run --s="source" --t="target"', '').argv

if (options['dry-run']) {
console.log(
yellow(
'vue2-migration-helper in dry-run mode, no files will be written to disk.'
)
)
}

const sourceGlob = resolve(options.source, '**/*.vue').replace('\\', '/')

Expand All @@ -34,12 +44,12 @@ glob(sourceGlob, (err, sources) => {
sources.forEach(source => {
console.log(green(`Processing: ${source}`))

const content = vue2MigrationHelper({
path: source
// TODO: pass target file
vue2MigrationHelper({
source: source,
dryRun: options.dryRun === true ? true : false
})

console.log(content)

// fs.writeFileSync(path, content)
console.log(green('Succesffuly processed file: ', source))
})
})
16 changes: 13 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { types } from '@babel/core'
import fs from 'fs'
import { getAst, getCode } from './lib/astUtilities'
import { addBody } from './lib/generators/body'
import { addImports } from './lib/generators/imports'
Expand All @@ -9,8 +10,8 @@ import { updateVueObjectReferences } from './lib/transformers/vueObjectReference

type Options = {
source: string
target: string
dryRun: boolean
target?: string
dryRun?: boolean
}

export function vue2MigrationHelper({
Expand All @@ -31,14 +32,23 @@ export function vue2MigrationHelper({
// add body
outputAst = addBody(outputAst)

console.log(getCode(outputAst))

// update template refs
outputAst = updateTemplateRefs(outputAst)

// update vue object this references
outputAst = updateVueObjectReferences(outputAst)

// get final code
const code = getOutputTemplate(originalTemplate, getCode(outputAst))

if (!dryRun && target) {
fs.writeFileSync(target, code)
}

// return final code
return getOutputTemplate(originalTemplate, getCode(outputAst))
return code
}

export default {
Expand Down
3 changes: 3 additions & 0 deletions src/lib/transformers/templateRefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export function updateTemplateRefs(ast: types.File) {
// addd ref(null) for each template ref
const setupMethodBody = getSetupMethod(ast).body.body
const returnStatement = setupMethodBody.slice(-1)[0] as types.ReturnStatement

console.log(setupMethodBody, returnStatement)

const argument = returnStatement.argument as types.ObjectExpression

templateRefList.forEach(templateRef => {
Expand Down

0 comments on commit 7087ecf

Please sign in to comment.