Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@magidoc/cli

Package Overview
Dependencies
Maintainers
1
Versions
123
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@magidoc/cli - npm Package Compare versions

Comparing version 0.2.3 to 0.3.0

build/template/fetch.js

2

build/commands/generate/command.d.ts
import { Command } from 'commander';
export default function buildGenerateCommand(program: Command): void;
export default function buildGenerateCommand(program: Command, version: string): void;
import { Option } from 'commander';
import generate from './index.js';
import { availableTemplates } from '../../template/index.js';
import { parseKeyValuePair } from '../../utils/args.js';
function buildGenerateCommand(program) {
function buildGenerateCommand(program, version) {
program
.command('generate')
.description("Generates a full static website using a template. Using this option doesn't give you the ability to customize the output website. If you wish to customize the website, use the init command to generate a fresh project.")
.option('-o|--output [destination]', 'Specifies the output directory of the built website', './build')
.addOption(new Option('-t|--template <name>', 'Specifies the target template')
.choices(availableTemplates())
.default(availableTemplates()[0]))
.addOption(new Option('-e|--template-version <version>', 'Specifies the target template version. This defaults to the current CLI version. Note that there may be issues when fetching templates from a different version from the current one, so use this option at your own risk.').default(version))
.option('-o|--output <destination>', 'Specifies the output directory of the built website', './build')
.addOption(new Option('-u|--url <url>', 'Specifies the target GraphQL API to fetch the schema from using the introspection query'))

@@ -20,4 +25,6 @@ .addOption(new Option('-m|--method <method>', 'The HTTP method to use to fetch the GraphQL Schema')

]))
.action(({ output, url, method, header }) => {
generate({
.action(async ({ output, url, method, header, template, templateVersion, }) => {
await generate({
template,
templateVersion,
output,

@@ -24,0 +31,0 @@ fetchConfig: {

@@ -0,4 +1,13 @@

import type { Template } from '../../template';
import type { FetchConfig } from './schema/fetch';
export declare type GenerationConfig = {
/**
* The target template for generation
*/
template: Template;
/**
* The template version to use for generation
*/
templateVersion: string;
/**
* The configuration used for fetching the GraphQL Schema from the remote server

@@ -12,2 +21,2 @@ */

};
export default function generate(config: GenerationConfig): void;
export default function generate(config: GenerationConfig): Promise<void>;

@@ -1,3 +0,19 @@

function generate(config) {
console.log(JSON.stringify(config));
import fetchTemplate from '../../template/fetch.js';
import { tmpTemplateArchiveFile, tmpTemplateDirectory } from '../../template/tmp.js';
import { unzipTemplate } from '../../template/unzip.js';
async function generate(config) {
const tmpArchive = tmpTemplateArchiveFile();
const tmpDirectory = tmpTemplateDirectory();
await fetchTemplate({
template: config.template,
version: config.templateVersion,
destination: tmpArchive.path,
});
console.log(`Output zip file to ${tmpArchive.path}`);
await unzipTemplate({
zipLocation: tmpArchive.path,
destination: tmpDirectory.path,
});
console.log(`Unzipped directory to ${tmpDirectory.path}`);
}

@@ -4,0 +20,0 @@

@@ -0,1 +1,2 @@

#! /usr/bin/env node
export {};

@@ -0,1 +1,2 @@

#! /usr/bin/env node
import { Command } from 'commander';

@@ -15,5 +16,5 @@ import { readFileSync } from 'fs';

.version(version);
buildGenerateCommand(program);
buildGenerateCommand(program, version);
buildInitCommand(program);
program.parse();
//# sourceMappingURL=index.js.map

@@ -0,6 +1,7 @@

import type { Template } from '.';
export declare type FetchTemplateConfig = {
name: string;
template: Template;
version: string;
destination: string;
};
export default function fetchTemplate(config: FetchTemplateConfig): void;
export default function fetchTemplate(config: FetchTemplateConfig): Promise<void>;

@@ -5,3 +5,3 @@ {

"private": false,
"version": "0.2.3",
"version": "0.3.0",
"type": "module",

@@ -11,5 +11,11 @@ "license": "MIT",

"types": "./build/index.d.ts",
"bin": {
"magidoc": "./build/index.js"
},
"dependencies": {
"@magidoc/rollup-plugin-fetch-gql-schema": "^0.2.3",
"commander": "^9.1.0"
"@magidoc/rollup-plugin-fetch-gql-schema": "^0.3.0",
"axios": "^0.26.1",
"commander": "^9.1.0",
"extract-zip": "^2.0.1",
"tmp": "^0.2.1"
},

@@ -19,2 +25,3 @@ "devDependencies": {

"@types/jest": "^27.4.1",
"@types/tmp": "^0.2.3",
"esbuild": "^0.14.34",

@@ -25,2 +32,3 @@ "jest": "^27.5.1",

"rollup": "^2.70.1",
"rollup-plugin-preserve-shebang": "^1.0.1",
"ts-jest": "^27.1.4",

@@ -27,0 +35,0 @@ "tslib": "^2.3.1",

import { Command, Option } from 'commander'
import generate from '.'
import { availableTemplates, Template } from '../../template'
import { KeyValue, parseKeyValuePair } from '../../utils/args'

@@ -7,2 +8,4 @@ import type { Method } from './schema/fetch'

type GenerateCommandOptions = {
template: Template
templateVersion: string
output: string

@@ -14,3 +17,6 @@ url: string

export default function buildGenerateCommand(program: Command) {
export default function buildGenerateCommand(
program: Command,
version: string,
) {
program

@@ -21,4 +27,15 @@ .command('generate')

)
.addOption(
new Option('-t|--template <name>', 'Specifies the target template')
.choices(availableTemplates())
.default(availableTemplates()[0]),
)
.addOption(
new Option(
'-e|--template-version <version>',
'Specifies the target template version. This defaults to the current CLI version. Note that there may be issues when fetching templates from a different version from the current one, so use this option at your own risk.',
).default(version),
)
.option(
'-o|--output [destination]',
'-o|--output <destination>',
'Specifies the output directory of the built website',

@@ -52,12 +69,23 @@ './build',

)
.action(({ output, url, method, header }: GenerateCommandOptions) => {
generate({
.action(
async ({
output,
fetchConfig: {
url,
headers: header,
method,
},
})
})
url,
method,
header,
template,
templateVersion,
}: GenerateCommandOptions) => {
await generate({
template,
templateVersion,
output,
fetchConfig: {
url,
headers: header,
method,
},
})
},
)
}

@@ -0,1 +1,8 @@

import type { Template } from '../../template'
import fetchTemplate from '../../template/fetch'
import {
tmpTemplateArchiveFile,
tmpTemplateDirectory,
} from '../../template/tmp'
import { unzipTemplate } from '../../template/unzip'
import type { FetchConfig } from './schema/fetch'

@@ -5,2 +12,12 @@

/**
* The target template for generation
*/
template: Template
/**
* The template version to use for generation
*/
templateVersion: string
/**
* The configuration used for fetching the GraphQL Schema from the remote server

@@ -16,4 +33,20 @@ */

export default function generate(config: GenerationConfig) {
console.log(JSON.stringify(config))
export default async function generate(config: GenerationConfig) {
const tmpArchive = tmpTemplateArchiveFile()
const tmpDirectory = tmpTemplateDirectory()
await fetchTemplate({
template: config.template,
version: config.templateVersion,
destination: tmpArchive.path,
})
console.log(`Output zip file to ${tmpArchive.path}`)
await unzipTemplate({
zipLocation: tmpArchive.path,
destination: tmpDirectory.path,
})
console.log(`Unzipped directory to ${tmpDirectory.path}`)
}

@@ -0,1 +1,3 @@

#! /usr/bin/env node
import { Command } from 'commander'

@@ -24,5 +26,5 @@ import { readFileSync } from 'fs'

buildGenerateCommand(program)
buildGenerateCommand(program, version)
buildInitCommand(program)
program.parse()

@@ -0,3 +1,7 @@

import fs, { type WriteStream } from 'fs'
import axios, { AxiosError } from 'axios'
import type { Template } from '.'
export type FetchTemplateConfig = {
name: string
template: Template
version: string

@@ -7,4 +11,52 @@ destination: string

export default function fetchTemplate(config: FetchTemplateConfig) {
console.log(config)
const templateUrl =
'https://github.com/magidoc-org/magidoc/releases/download/<version>/starter-<name>.zip'
export default async function fetchTemplate(config: FetchTemplateConfig) {
const targetUrl = templateUrl
.replace('<version>', config.version)
.replace('<name>', config.template)
await axios
.request<{
pipe: (stream: WriteStream) => void
}>({
method: 'get',
url: targetUrl,
responseType: 'stream',
})
.then(async (response) => {
const writer = fs.createWriteStream(config.destination)
return new Promise((resolve, reject) => {
let error: Error
writer.on('error', (error) => {
error = error
writer.close()
reject(error)
})
writer.on('close', () => {
if (!error) {
resolve(true)
}
})
response.data.pipe(writer)
})
})
.catch((error: AxiosError) => {
if (error.response) {
if (error.response.status === 404) {
throw new Error(
`Could not find template '${config.template}' with version '${config.version}'. \nSearched the following location: \n - ${targetUrl}`,
)
}
}
throw new Error(
`Unable to fetch template at ${targetUrl}. Cause: ${error.message}`,
)
})
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc