@spaship/sync-service
Advanced tools
Comparing version 0.0.13 to 0.0.14
@@ -0,4 +1,8 @@ | ||
const fsp = require("fs").promises; | ||
const path = require("path"); | ||
const decompress = require("decompress"); | ||
const config = require("../config"); | ||
const mvdir = require("mvdir"); | ||
const tmp = require("tmp-promise"); | ||
const common = require("@spaship/common"); | ||
const { write: writeMetadata } = require("./metadata"); | ||
@@ -11,23 +15,66 @@ | ||
// remove starting slashes in paths | ||
const appPathNoSlash = appPath.replace(/^\//, ""); | ||
const destDir = path.resolve(config.get("webroot"), appPathNoSlash); | ||
// create a dir in the tmp_dir location, but keep the random archive name | ||
let tmpDir = `${spaArchive}-extracted`; | ||
await fsp.mkdir(tmpDir); | ||
// const tmpDir = await fsp.m({ template: path.join(config.get("tmp_dir"), " }); | ||
console.log(`created tmp dir ${tmpDir}`); | ||
// extract the archive | ||
try { | ||
await decompress(spaArchive, destDir); | ||
await decompress(spaArchive, tmpDir); | ||
// detect if the archive was created with `npm pack`. npm pack creates a | ||
// tarball with a "package" dir. we want what's in the package dir but not | ||
// the dir itself. | ||
let isNpmPack; | ||
try { | ||
const packageStat = await fsp.lstat(path.join(tmpDir, "package")); | ||
isNpmPack = packageStat.isDirectory(); | ||
} catch (e) { | ||
isNpmPack = false; | ||
} | ||
// if the archive was generated by `npm pack`, move into the "package" | ||
// directory where all the goodies are. | ||
if (isNpmPack) { | ||
console.log(`detected npm pack archive, moving into package/`); | ||
tmpDir = path.join(tmpDir, "package"); | ||
} | ||
let spaConfig = { name, path: appPath }; | ||
let hasYaml = false; | ||
const yamlFilePath = path.join(tmpDir, "spaship.yaml"); | ||
try { | ||
spaConfig = await common.config.read(yamlFilePath); | ||
hasYaml = true; | ||
} catch (e) {} | ||
const valid = common.config.validate(spaConfig); | ||
console.log("VALID?", valid); | ||
if (!valid) { | ||
throw new Error("spa config is not valid"); | ||
} | ||
// remove starting slashes in paths | ||
const flatPath = common.flatpath.toDir(spaConfig.path); | ||
const destDir = path.join(config.get("webroot"), flatPath); | ||
// move the goodies dir to the final destination in the webroot | ||
console.log(`moving from ${tmpDir} to ${destDir}`); | ||
await mvdir(tmpDir, destDir); | ||
// write spa metadata to filesystem | ||
writeMetadata({ | ||
appName: name, | ||
appPath: destDir, | ||
type: "ref", | ||
value: ref | ||
}); | ||
writeMetadata({ | ||
appName: name, | ||
appPath: destDir, | ||
type: "name", | ||
value: name | ||
}); | ||
if (!hasYaml) { | ||
writeMetadata(path.join(destDir, "spaship.yaml"), { | ||
name, | ||
path: spaConfig.path | ||
}); | ||
} | ||
if (ref) { | ||
writeMetadata(path.join(destDir, "spaship.yaml"), { | ||
ref | ||
}); | ||
} | ||
@@ -34,0 +81,0 @@ console.log(`[deploy] extracted "${name}" to ${destDir}, deploy complete.`); |
const path = require("path"); | ||
const fsp = require("fs").promises; | ||
const config = require("../config"); | ||
const common = require("@spaship/common"); | ||
const { flow, map, filter } = require("lodash/fp"); | ||
@@ -13,23 +14,4 @@ | ||
async function write({ appName, appPath, type, value } = {}) { | ||
const appMetaDir = getMetaDir(appPath); | ||
const filePath = path.resolve(appMetaDir, type); | ||
try { | ||
await fsp.mkdir(appMetaDir); | ||
} catch (e) { | ||
if (e.code !== "EEXIST") { | ||
console.error(e); | ||
return; | ||
} | ||
} | ||
try { | ||
await fsp.writeFile(filePath, value); | ||
console.log( | ||
`[deploy] wrote ${appName}'s "${type}" metadata to ${filePath}` | ||
); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
async function write(filename, extraData) { | ||
await common.config.append(filename, extraData); | ||
} | ||
@@ -75,13 +57,5 @@ | ||
// read the contents of the ref and name files | ||
const [ref, name] = await Promise.all([ | ||
readMetaFile(spaDir, "ref"), | ||
readMetaFile(spaDir, "name") | ||
]); | ||
return { | ||
path: `/${spaDir}`, | ||
ref, | ||
name | ||
}; | ||
return await common.config.read(path.join(spaDir, "spaship.yaml")); | ||
} | ||
module.exports = { write, getAll, get }; |
{ | ||
"name": "@spaship/sync-service", | ||
"version": "0.0.13", | ||
"version": "0.0.14", | ||
"description": "The file synchronization service for the SPAship platform.", | ||
@@ -31,2 +31,3 @@ "homepage": "https://github.com/spaship/sync-service", | ||
"dependencies": { | ||
"@spaship/common": "^0.0.0", | ||
"axios": "^0.19.0", | ||
@@ -42,3 +43,5 @@ "cors": "^2.8.5", | ||
"multer": "^1.4.2", | ||
"mvdir": "^1.0.5", | ||
"nconf": "^0.10.0", | ||
"tmp-promise": "^2.0.2", | ||
"url-join": "^4.0.1" | ||
@@ -53,21 +56,3 @@ }, | ||
"standard-version": "7.0.0" | ||
}, | ||
"eslintConfig": { | ||
"extends": [ | ||
"prettier" | ||
], | ||
"env": { | ||
"shared-node-browser": true | ||
}, | ||
"parserOptions": { | ||
"ecmaVersion": 9, | ||
"sourceType": "module" | ||
}, | ||
"rules": { | ||
"prettier/prettier": "error" | ||
}, | ||
"plugins": [ | ||
"prettier" | ||
] | ||
} | ||
} |
const path = require("path"); | ||
const fsp = require("fs").promises; | ||
const decompress = require("decompress"); | ||
@@ -8,22 +9,28 @@ const multer = require("multer"); | ||
const multerUpload = multer({ | ||
dest: config.get("upload_dir"), | ||
fileFilter: (req, file, cb) => { | ||
// to reject file uploads: cb(null, false); | ||
cb(null, true); | ||
} | ||
}); | ||
// return an array of expressjs callbacks, the first using multer to support | ||
// uploading multipart forms (ie, files), and the second to handle extraction | ||
function createDeployMiddleware() { | ||
const multerUpload = multer({ | ||
dest: config.get("upload_dir"), | ||
fileFilter: (req, file, cb) => { | ||
// to reject file uploads: cb(null, false); | ||
cb(null, true); | ||
} | ||
}); | ||
const upload = multerUpload.single("upload"); | ||
const extract = (req, res) => { | ||
const extract = (req, res, next) => { | ||
const { name, path: appPath, ref } = req.body; | ||
const { path: spaArchive } = req.file; | ||
deploy({ name, spaArchive, appPath, ref }); | ||
res.send("SPA uploaded, deployment continuing in the background."); | ||
deploy({ name, spaArchive, appPath, ref }) | ||
.then(result => { | ||
console.log(`extracted successfully`); | ||
res.send("SPA uploaded successfully."); | ||
}) | ||
.catch(err => { | ||
res.status(403); | ||
console.log(err); | ||
}); | ||
}; | ||
@@ -30,0 +37,0 @@ |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
33306
19
405
0
15
5
+ Added@spaship/common@^0.0.0
+ Addedmvdir@^1.0.5
+ Addedtmp-promise@^2.0.2
+ Added@spaship/common@0.0.0(transitive)
+ Addedajv@6.12.6(transitive)
+ Addedargparse@1.0.10(transitive)
+ Addedesprima@4.0.1(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedfs-extra@8.1.0(transitive)
+ Addedjs-yaml@3.14.1(transitive)
+ Addedjson-schema-traverse@0.4.1(transitive)
+ Addedjsonfile@4.0.0(transitive)
+ Addedmvdir@1.0.22(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedsprintf-js@1.0.3(transitive)
+ Addedtmp@0.1.0(transitive)
+ Addedtmp-promise@2.1.1(transitive)
+ Addeduniversalify@0.1.2(transitive)
+ Addeduri-js@4.4.1(transitive)