Created
January 28, 2022 14:58
-
-
Save liesislukas/bca20d7ce621dc9109c38079e43158a2 to your computer and use it in GitHub Desktop.
Framer component to zip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs"); | |
const path = require("path"); | |
const del = require("del"); | |
const util = require("util"); | |
const exec = util.promisify(require("child_process").exec); | |
const readFile = util.promisify(fs.readFile); | |
const writeOutput = require("./writeOutput"); | |
const express = require("express"); | |
const axios = require("axios"); | |
const app = express(); | |
const port = 3598; | |
const acorn = require("acorn-loose"); | |
const walk = require("acorn-walk"); | |
app.get("/get.zip", async (req, res) => { | |
const outputDir = path.join(__dirname, "./tmp/out"); | |
async function getContents({ url, componentName, innerImportModule }) { | |
console.log(`#202227152933400 url:`, url); | |
console.log(`#202227152938211 componentName:`, componentName); | |
let content = (await axios.get(url)).data; | |
const framerUrls = [ | |
"https://framer.com/m/", | |
"https://framerusercontent.com/", | |
"https://ga.jspm.io/", | |
"https://jspm.dev/", | |
]; | |
const foundImportUrls = []; | |
walk.full(acorn.parse(content), (node) => { | |
if (node.type === "ImportDeclaration") { | |
let url = node.source.value; | |
framerUrls.forEach((framerUrl) => { | |
if (url.indexOf(framerUrl) === 0) { | |
let localUrl = url.replace("https://", ""); | |
localUrl = localUrl.split(".js@")[0]; | |
foundImportUrls.push({ | |
url, | |
localUrl: `./${localUrl}`, | |
}); | |
content = content | |
.split(url) | |
.join(`jsx/${componentName}/${localUrl}`); | |
} | |
}); | |
} | |
}); | |
await Promise.all( | |
foundImportUrls.map(async (foundImport) => { | |
await getContents({ | |
url: foundImport.url, | |
innerImportModule: foundImport.localUrl, | |
componentName, | |
}); | |
}) | |
); | |
if (componentName) { | |
innerImportModule = innerImportModule || ""; | |
await writeOutput({ | |
path: path.join( | |
outputDir, | |
componentName, | |
innerImportModule, | |
"index.js" | |
), | |
content: content, | |
}); | |
} | |
} | |
const url = "https://framer.com/m/Lukas1-wR43.js@KE7RLayPhvVdb801Axxn"; | |
let componentName = url.split("https://framer.com/m/")[1].split("-")[0]; | |
await getContents({ url, componentName }); | |
await exec(`cd ${outputDir} && zip -qr ../build.zip .`); | |
let zipFile = path.join(outputDir, "../", `./build.zip`); | |
let data = await readFile(zipFile); | |
const toDelete = path.join(outputDir, "../"); | |
await del([toDelete], { force: true }); | |
const length = data ? data.byteLength : 0; | |
res.set("Content-Disposition", "build.zip"); | |
res.set("Content-Length", length); | |
res.set("content-type", "application/octet-stream"); | |
return res.send(data); | |
}); | |
app.listen(port, () => { | |
console.log(`Example app listening on port http://localhost:${port}`); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const _path = require("path"); | |
const fs = require("fs"); | |
const mkdirp = require("mkdirp"); | |
// if content is not provided, then provide template + payload (if any) | |
function writeOutput({ path, content }) { | |
return new Promise((resolve) => { | |
const finalPath = _path.join(path); | |
mkdirp.sync(_path.dirname(finalPath)); | |
// 'w' - Open file for writing. The file is created (if it does not exist) or truncated (if it exists). | |
// 'wx' - Like 'w' but fails if path exists. | |
fs.writeFile(finalPath, content, { flag: "wx" }, (err) => { | |
resolve({ error: null, data: { success: !err } }); | |
}); | |
}); | |
} | |
module.exports = writeOutput; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment