@dynamicweb/cli
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -38,6 +38,15 @@ import fetch from 'node-fetch'; | ||
}) | ||
.option('overwrite', { | ||
alias: 'o', | ||
type: 'boolean', | ||
describe: 'Used with import, will overwrite existing files at destrination if set to true' | ||
}) | ||
.option('createEmpty', { | ||
type: 'boolean', | ||
describe: 'Used with import, will create a file even if its empty' | ||
}) | ||
.option('includeFiles', { | ||
alias: 'f', | ||
type: 'boolean', | ||
describe: 'Includes files in list of directories and files' | ||
describe: 'Used with export, includes files in list of directories and files' | ||
}) | ||
@@ -47,7 +56,7 @@ .option('recursive', { | ||
type: 'boolean', | ||
describe: 'Handles all directories recursively' | ||
describe: 'Used with list, import and export, handles all directories recursively' | ||
}) | ||
.option('raw', { | ||
type: 'boolean', | ||
describe: 'Keeps zip file instead of unpacking it' | ||
describe: 'Used with export, keeps zip file instead of unpacking it' | ||
}) | ||
@@ -96,4 +105,15 @@ .option('iamstupid', { | ||
if (argv.dirPath && argv.outPath) { | ||
let resolvedPath = path.resolve(argv.dirPath) | ||
await uploadFile(env, user, resolvedPath, argv.outPath); | ||
let resolvedPath = path.resolve(argv.dirPath); | ||
let files; | ||
if (!argv.overwrite) { | ||
files = (await getFilesStructure(env, user, argv.outPath, argv.recursive, true)).model; | ||
} | ||
if (argv.recursive) { | ||
await processDirectory(env, user, resolvedPath, argv.outPath, files, resolvedPath, argv.createEmpty, true); | ||
} else { | ||
let filesInDir = getFilesInDirectory(resolvedPath); | ||
if (files) | ||
filesInDir = getFilesNotInData(filesInDir, files.files.data, resolvedPath); | ||
await uploadFiles(env, user, filesInDir, argv.outPath, argv.createEmpty); | ||
} | ||
} | ||
@@ -103,2 +123,40 @@ } | ||
function convertToDataFormat(filePath, resolvedPath) { | ||
const relativePath = `/Files${filePath.substring(resolvedPath.length)}`; | ||
return path.format(path.parse(relativePath)).replace(/\\/g, '/'); | ||
} | ||
function getFilesNotInData(filesInDir, data, resolvedPath) { | ||
const existingPaths = data.map(file => file.filePath); | ||
return filesInDir.filter(filePath => { | ||
const convertedPath = convertToDataFormat(filePath, resolvedPath); | ||
return !existingPaths.includes(convertedPath); | ||
}); | ||
} | ||
function getFilesInDirectory(dirPath) { | ||
return fs.readdirSync(dirPath) | ||
.map(file => path.join(dirPath, file)) | ||
.filter(file => fs.statSync(file).isFile()); | ||
} | ||
async function processDirectory(env, user, dirPath, outPath, files, originalDir, createEmpty, isRoot = false) { | ||
let filesInDir = getFilesInDirectory(dirPath); | ||
let missingFiles; | ||
if (files === undefined) | ||
missingFiles = filesInDir; | ||
else | ||
missingFiles = getFilesNotInData(filesInDir, files.files.data, originalDir); | ||
if (missingFiles.length > 0) | ||
await uploadFiles(env, user, missingFiles, isRoot ? outPath : path.join(outPath, path.basename(dirPath)), createEmpty); | ||
const subDirectories = fs.readdirSync(dirPath) | ||
.map(subDir => path.join(dirPath, subDir)) | ||
.filter(subDir => fs.statSync(subDir).isDirectory()); | ||
for (let subDir of subDirectories) { | ||
const remoteSubDir = files?.directories.find(dir => dir.name === path.basename(subDir)); | ||
await processDirectory(env, user, subDir, isRoot ? outPath : path.join(outPath, path.basename(dirPath)), remoteSubDir, originalDir, createEmpty); | ||
} | ||
} | ||
function resolveTree(dirs, indentLevel, parentHasFiles) { | ||
@@ -215,8 +273,11 @@ let end = `└──` | ||
export async function uploadFile(env, user, localFilePath, destinationPath) { | ||
console.log('Uploading file') | ||
export async function uploadFiles(env, user, localFilePaths, destinationPath, createEmpty = false) { | ||
console.log('Uploading files') | ||
let form = new FormData(); | ||
form.append('path', destinationPath); | ||
form.append('files', fs.createReadStream(localFilePath)); | ||
let res = await fetch(`${env.protocol}://${env.host}/Admin/Api/Upload`, { | ||
localFilePaths.forEach((localPath, index) => { | ||
console.log(localPath) | ||
form.append('files', fs.createReadStream(path.resolve(localPath))); | ||
}); | ||
let res = await fetch(`${env.protocol}://${env.host}/Admin/Api/Upload?` + new URLSearchParams({"createEmptyFiles": createEmpty}), { | ||
method: 'POST', | ||
@@ -231,3 +292,3 @@ body: form, | ||
if (env.verbose) console.log(await res.json()) | ||
console.log(`File uploaded`) | ||
console.log(`Files uploaded`) | ||
} | ||
@@ -234,0 +295,0 @@ else { |
@@ -5,3 +5,3 @@ import fetch from 'node-fetch'; | ||
import { setupUser } from './login.js'; | ||
import { uploadFile } from './files.js'; | ||
import { uploadFiles } from './files.js'; | ||
@@ -29,3 +29,3 @@ export function installCommand() { | ||
let resolvedPath = path.resolve(argv.filePath) | ||
await uploadFile(env, user, resolvedPath, 'System/AddIns/Local'); | ||
await uploadFiles(env, user, [resolvedPath], 'System/AddIns/Local'); | ||
await installAddin(env, user, resolvedPath) | ||
@@ -32,0 +32,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"description": "Dynamicweb CLI is a commandline tool for interacting with Dynamicweb 10 solutions.", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"main": "bin/index.js", | ||
@@ -8,0 +8,0 @@ "files": [ |
49787
1067