myst-templates
Advanced tools
Comparing version 1.0.3 to 1.0.4
import fs, { createWriteStream, mkdirSync } from 'node:fs'; | ||
import { dirname, join, parse } from 'node:path'; | ||
import { join, parse, sep } from 'node:path'; | ||
import { createHash } from 'node:crypto'; | ||
import AdmZip from 'adm-zip'; | ||
import { glob } from 'glob'; | ||
import yaml from 'js-yaml'; | ||
import { copyFileMaintainPath, isDirectory } from 'myst-cli-utils'; | ||
import { TemplateKind } from 'myst-common'; | ||
@@ -104,19 +106,30 @@ import fetch from 'node-fetch'; | ||
*/ | ||
function unnestTemplate(path) { | ||
async function unnestTemplate(session, path) { | ||
var _a; | ||
const content = fs.readdirSync(path); | ||
if (!content.includes(TEMPLATE_YML)) { | ||
content.forEach((dir) => { | ||
var _a; | ||
const templateYmlFile = join(path, dir, TEMPLATE_YML); | ||
if (fs.existsSync(templateYmlFile)) { | ||
fs.copyFileSync(templateYmlFile, join(path, TEMPLATE_YML)); | ||
const templateYml = yaml.load(fs.readFileSync(templateYmlFile).toString()); | ||
(_a = templateYml.files) === null || _a === void 0 ? void 0 : _a.forEach((file) => { | ||
const source = join(path, dir, ...file.split('/')); | ||
const dest = join(path, ...file.split('/')); | ||
fs.mkdirSync(dirname(dest), { recursive: true }); | ||
fs.copyFileSync(source, dest); | ||
}); | ||
} | ||
}); | ||
if (content.includes(TEMPLATE_YML)) | ||
return; | ||
let nestedPath; | ||
content.forEach((dir) => { | ||
const templateYmlFile = join(path, dir, TEMPLATE_YML); | ||
if (!nestedPath && fs.existsSync(templateYmlFile)) { | ||
nestedPath = join(path, dir); | ||
} | ||
}); | ||
if (!nestedPath) | ||
return; | ||
const templateYmlFile = join(nestedPath, TEMPLATE_YML); | ||
fs.copyFileSync(templateYmlFile, join(path, TEMPLATE_YML)); | ||
const templateYml = yaml.load(fs.readFileSync(templateYmlFile).toString()); | ||
if ((_a = templateYml.files) === null || _a === void 0 ? void 0 : _a.length) { | ||
await Promise.all(templateYml.files.map(async (file) => { | ||
const resolvedEntry = [...nestedPath.split(sep), file].join('/'); | ||
const matches = await glob(resolvedEntry); | ||
matches | ||
.map((match) => match.split('/').join(sep)) | ||
.filter((match) => !isDirectory(match)) | ||
.forEach((match) => { | ||
copyFileMaintainPath(session, match, nestedPath, path); | ||
}); | ||
})); | ||
} | ||
@@ -179,3 +192,3 @@ } | ||
zip.extractAllTo(templatePath); | ||
unnestTemplate(templatePath); | ||
await unnestTemplate(session, templatePath); | ||
} | ||
@@ -182,0 +195,0 @@ export async function fetchPublicTemplate(session, name, kind) { |
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import { hashAndCopyStaticFile } from 'myst-cli-utils'; | ||
import { globSync } from 'glob'; | ||
import { hashAndCopyStaticFile, isDirectory } from 'myst-cli-utils'; | ||
import { TemplateOptionType } from 'myst-common'; | ||
@@ -423,7 +424,9 @@ import { PAGE_FRONTMATTER_KEYS, RESERVED_EXPORT_KEYS, validateAuthor, validateGithubUrl, validateLicenses, validatePageFrontmatter, } from 'myst-frontmatter'; | ||
if (file && opts.templateDir) { | ||
const filePath = path.resolve(opts.templateDir, ...file.split('/')); | ||
if (!fs.existsSync(filePath)) { | ||
const filePath = [...opts.templateDir.split(path.sep), file].join('/'); | ||
const matches = globSync(filePath).map((match) => match.split('/').join(path.sep)); | ||
const files = matches.filter((match) => !isDirectory(match)); | ||
if (!matches.length) { | ||
validationError(`file does not exist: ${filePath}`, fileOpts); | ||
} | ||
else if (fs.lstatSync(filePath).isDirectory()) { | ||
else if (!files.length) { | ||
validationError(`file must not be directory: ${filePath}`, fileOpts); | ||
@@ -430,0 +433,0 @@ } |
{ | ||
"name": "myst-templates", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "API types and scripts for downloading MyST Templates", | ||
@@ -42,5 +42,6 @@ "author": "Rowan Cockett <rowan@curvenote.com>", | ||
"commander": "^10.0.1", | ||
"glob": "^10.3.1", | ||
"js-yaml": "^4.1.0", | ||
"myst-cli-utils": "^2.0.1", | ||
"myst-common": "^1.0.3", | ||
"myst-cli-utils": "^2.0.2", | ||
"myst-common": "^1.0.4", | ||
"myst-frontmatter": "^1.0.3", | ||
@@ -47,0 +48,0 @@ "node-fetch": "^3.3.1", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
57486
1165
10
+ Addedglob@^10.3.1
+ Added@isaacs/cliui@8.0.2(transitive)
+ Added@pkgjs/parseargs@0.11.0(transitive)
+ Addedansi-regex@5.0.16.1.0(transitive)
+ Addedansi-styles@4.3.06.2.1(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@2.0.1(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedcross-spawn@7.0.6(transitive)
+ Addedeastasianwidth@0.2.0(transitive)
+ Addedemoji-regex@8.0.09.2.2(transitive)
+ Addedforeground-child@3.3.0(transitive)
+ Addedglob@10.4.5(transitive)
+ Addedis-fullwidth-code-point@3.0.0(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedjackspeak@3.4.3(transitive)
+ Addedlru-cache@10.4.3(transitive)
+ Addedminimatch@9.0.5(transitive)
+ Addedminipass@7.1.2(transitive)
+ Addedpackage-json-from-dist@1.0.1(transitive)
+ Addedpath-key@3.1.1(transitive)
+ Addedpath-scurry@1.11.1(transitive)
+ Addedshebang-command@2.0.0(transitive)
+ Addedshebang-regex@3.0.0(transitive)
+ Addedsignal-exit@4.1.0(transitive)
+ Addedstring-width@4.2.35.1.2(transitive)
+ Addedstrip-ansi@6.0.17.1.0(transitive)
+ Addedwhich@2.0.2(transitive)
+ Addedwrap-ansi@7.0.08.1.0(transitive)
Updatedmyst-cli-utils@^2.0.2
Updatedmyst-common@^1.0.4