const _ = require('lodash')
const parseImports = require('parse-es6-imports')
const contents = `import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import {
Pane as EGPane,
Card
} from 'evergreen-layers'
import TextInput from 'evergreen-text-input'
import SelectMenu from 'evergreen-select-menu'
import { Text } from 'evergreen-typography'
import DropdownButton from './DropdownButton'`
// find import
// find export
// get string of everything in between
function ImportStatement({ startLineIndex }) {
this.startLineIndex = startLineIndex
this.endLineIndex = null
this.isComplete = false
this.hasFromWord = false
this.words = ['import']
this.package = null
this.exports = []
}
ImportStatement.prototype.isEvergreenPackage = function() {
return this.package.includes('evergreen-')
}
ImportStatement.prototype.process = function() {
let isDefaultExport = true
let isAlias = false
for (let i = 0; i < this.words.length; i++) {
const word = this.words[i]
if (word === 'import') continue
if (word === 'from') break
if (word === '}') break
if (word === '{') {
isDefaultExport = false
continue
}
if (isDefaultExport) {
this.exports.push({ isDefaultExport: true, name: word })
break
}
if (word === 'as') {
isAlias = true
continue
}
if (isAlias) {
this.exports[this.exports.length - 1].alias = word.replace(',', '')
isAlias = false
continue
}
if (word) {
this.exports.push({ name: word.replace(',', '') })
continue
}
}
return this
}
function findImportStatements(input) {
const lines = input.split('\n')
const imports = []
const lastStatement = () => imports[imports.length - 1]
lines.forEach((line, index) => {
const words = line.split(' ')
words.forEach((word, wordIndex) => {
if (word === 'import' && wordIndex === 0) {
imports.push(new ImportStatement({ startLineIndex: index }))
} else if (!lastStatement().isComplete) {
if (word === 'from') {
lastStatement().hasFromWord = true
lastStatement().words.push(word)
} else if (lastStatement().hasFromWord) {
lastStatement().package = word
lastStatement().isCompleted = true
lastStatement().endLineIndex = index
lastStatement().words.push(word)
} else if (!/\s/.test(word)) {
if (!Number.isNaN(word.charCodeAt())) {
// weird whitespace bug
lastStatement().words.push(word)
}
}
}
})
})
return { lines, imports }
}
const { lines, imports } = findImportStatements(contents)
const evergreenImports = imports
.filter(x => x.isEvergreenPackage())
.map(x => x.process())
function createNewImportStatement(inputImports) {
const stringBuilder = ['import', '{']
console.log(inputImports)
const exportItems = []
const result = inputImports
.reduce(
(arr, imp) => [
...arr,
imp.exports
.map(exp => {
if (exp.alias) {
return `${exp.name} as ${exp.alias}`
}
return exp.name
})
.join(', '),
],
[],
)
.join(', ')
console.log(result)
stringBuilder.push(result)
stringBuilder.push('}')
stringBuilder.push('from')
stringBuilder.push(`'evergreen-ui'`)
return stringBuilder.join(' ')
}
function shouldSkipLine(inputImports, lineIndex) {
let skip = false
inputImports.forEach(imp => {
if (lineIndex >= imp.startLineIndex && lineIndex <= imp.endLineIndex)
skip = true
})
return skip
}
const newLines = []
lines.forEach((line, index) => {
if (index === evergreenImports[0].startLineIndex) {
newLines.push(createNewImportStatement(evergreenImports))
} else if (!shouldSkipLine(evergreenImports, index)) {
newLines.push(line)
}
})
// Old
console.log(contents)
// New
console.log(newLines.join('\n'))