diff --git a/package-lock.json b/package-lock.json index 8db6cbf..2d9cadd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@awahlig/appfairy", - "version": "0.8.4", + "version": "0.8.5-alpha.8", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@awahlig/appfairy", - "version": "0.8.4", + "version": "0.8.5-alpha.8", "dependencies": { "babel-core": "^6.26.3", "babel-preset-env": "^1.7.0", diff --git a/package.json b/package.json index 5596f5e..e7ffd94 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@awahlig/appfairy", - "version": "0.8.4", + "version": "0.8.5-alpha.8", "description": "A CLI tool to Migrate a Webflow project into a React app", "main": "build/appfairy.js", "bin": { diff --git a/src/index.js b/src/index.js index f9586c0..b23a948 100644 --- a/src/index.js +++ b/src/index.js @@ -10,16 +10,17 @@ export const transpile = async (config) => { let outputFiles = [] await Promise.all([ - fs.readdir(config.input).then((files) => { - inputFiles = files + reread(config.input).then((files) => { + inputFiles = files.map((file) => path.relative(config.input, file)) }), git.removeAppfairyFiles().then((files) => { outputFiles.push(...files) }), ]) - const htmlFiles = inputFiles.filter(file => path.extname(file) == '.html') - const publicSubDirs = inputFiles.filter(file => !path.extname(file)) + const isHTML = (file) => path.extname(file) == '.html' + const htmlFiles = inputFiles.filter(isHTML) + const publicFiles = inputFiles.filter(file => !isHTML(file)) const scriptWriter = new ScriptWriter({ baseUrl: config.input, @@ -57,7 +58,7 @@ export const transpile = async (config) => { const makingPublicDir = makePublicDir( config, - publicSubDirs, + publicFiles, ).then((paths) => outputFiles.push(...paths)) await Promise.all([ @@ -83,7 +84,8 @@ const transpileHTMLFile = async ( const dataAttrs = $(':root').data() const viewWriter = new ViewWriter({ - name: htmlFile.split('.').slice(0, -1).join('.'), + folder: path.dirname(htmlFile), + name: path.basename(htmlFile).split('.').slice(0, -1).join('.'), baseUrl: config.baseUrl, source: config.source, }) @@ -96,13 +98,13 @@ const transpileHTMLFile = async ( return viewWriter } -const makePublicDir = async (config, publicSubDirs) => { +const makePublicDir = async (config, publicFiles) => { const publicDir = config.output.public - await Promise.all(publicSubDirs.map((publicSubDir) => { + await Promise.all(publicFiles.map((publicFile) => { return ncp( - `${config.input}/${publicSubDir}`, - `${publicDir}/${publicSubDir}`, + `${config.input}/${publicFile}`, + `${publicDir}/${publicFile}`, ) })) diff --git a/src/writers/view-writer.js b/src/writers/view-writer.js index 69bf480..4e82af8 100644 --- a/src/writers/view-writer.js +++ b/src/writers/view-writer.js @@ -31,6 +31,11 @@ const flattenChildren = (children = [], flatten = []) => { return flatten } +const dotRelative = (fromPath, toPath) => { + let result = path.relative(fromPath, toPath) + return result.startsWith('.') ? result : `./${result}` +} + @Internal(_) class ViewWriter extends Writer { static async writeAll(viewWriters, dir, ctrlsDir) { @@ -39,17 +44,17 @@ class ViewWriter extends Writer { const indexFilePath = `${dir}/index.js` const helpersFilePath = `${dir}/helpers.js` const childFilePaths = [indexFilePath, helpersFilePath] - ctrlsDir = path.relative(dir, ctrlsDir) - viewWriters = flattenChildren(viewWriters) const writingViews = viewWriters.map(async (viewWriter) => { const filePaths = await viewWriter.write(dir, ctrlsDir) childFilePaths.push(...filePaths) }) - const index = viewWriters.map((viewWriter) => { - return `export { default as ${viewWriter.className} } from './${viewWriter.className}'` - }).join('\n') + const index = flattenChildren(viewWriters + .filter((viewWriter) => viewWriter.folder == '.')) + .map((viewWriter) => ( + `export { default as ${viewWriter.className} } from './${viewWriter.className}'` + )).join('\n') const writingIndex = fs.writeFile(indexFilePath, freeLint(index)) const writingHelpers = fs.writeFile(helpersFilePath, raw.viewHelpers) @@ -63,6 +68,14 @@ class ViewWriter extends Writer { return childFilePaths } + get folder() { + return this[_].folder + } + + set folder(folder) { + this[_].folder = String(folder) + } + get baseUrl() { return this[_].baseUrl } @@ -169,6 +182,7 @@ class ViewWriter extends Writer { const child = new ViewWriter({ name: elName, html: $.html($el), + folder: this.folder, baseUrl: this.baseUrl, styles: this.styles, }) @@ -297,10 +311,11 @@ class ViewWriter extends Writer { this.name = options.name this.html = options.html this.source = options.source + this.folder = options.folder } async write(dir, ctrlsDir) { - const filePath = `${dir}/${this.className}.js` + const filePath = path.normalize(`${dir}/${this.folder}/${this.className}.js`) const childFilePaths = [filePath] const writingChildren = this[_].children.map(async (child) => { @@ -308,11 +323,14 @@ class ViewWriter extends Writer { childFilePaths.push(...filePaths) }) - const writingSelf = fs.writeFile(`${dir}/${this.className}.js`, this[_].compose(ctrlsDir)) + const writeSelf = async () => { + await fs.mkdir(path.dirname(filePath), { recursive: true }) + await fs.writeFile(filePath, this[_].compose(dir, ctrlsDir)) + } await Promise.all([ ...writingChildren, - writingSelf, + writeSelf(), ]) return childFilePaths @@ -340,10 +358,15 @@ class ViewWriter extends Writer { } } - _compose(ctrlsDir) { + _compose(dir, ctrlsDir) { + const helpersPath = dotRelative(this.folder, 'helpers') + const ctrlPath = dotRelative( + `${dir}/${this.folder}`, + `${ctrlsDir}/${this.folder}/${this.ctrlClassName}`, + ) return freeLint(` import React from 'react' - import { createScope, map, transformProxies } from './helpers' + import { createScope, map, transformProxies } from '${helpersPath}' ==>${this[_].composeChildImports()}<== const scripts = [ ==>${this[_].composeScriptsDeclerations()}<== @@ -356,7 +379,7 @@ class ViewWriter extends Writer { if (Controller) return Controller try { - Controller = require('${ctrlsDir}/${this.ctrlClassName}') + Controller = require('${ctrlPath}') Controller = Controller.default || Controller return Controller