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

@magicspace/core

Package Overview
Dependencies
Maintainers
5
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@magicspace/core - npm Package Compare versions

Comparing version 0.2.3 to 0.2.4

2

bld/library/@files/binary-file.d.ts

@@ -7,4 +7,4 @@ /// <reference types="node" />

content: Buffer;
constructor(path: string, possiblePathInProject: string);
constructor(path: string, context: File.FileContext);
toBuffer(): Buffer;
}

@@ -6,4 +6,4 @@ "use strict";

class BinaryFile extends file_1.File.File {
constructor(path, possiblePathInProject) {
super('binary', path, possiblePathInProject);
constructor(path, context) {
super('binary', path, context);
this.content = Buffer.alloc(0);

@@ -10,0 +10,0 @@ }

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

import { File } from '../file';
import { StructuredFile, StructuredFileOptions } from './structured-file';

@@ -6,4 +7,4 @@ export interface JSONFileOptions extends StructuredFileOptions {

content: TContent | undefined;
constructor(path: string, possiblePathInProject: string);
constructor(path: string, context: File.FileContext);
protected stringify(content: TContent | undefined): string;
}

@@ -6,4 +6,4 @@ "use strict";

class JSONFile extends structured_file_1.StructuredFile {
constructor(path, possiblePathInProject) {
super('json', path, possiblePathInProject);
constructor(path, context) {
super('json', path, context);
}

@@ -10,0 +10,0 @@ stringify(content) {

@@ -6,4 +6,4 @@ import { File } from '../file';

content: string;
constructor(path: string, possiblePathInProject: string);
constructor(path: string, context: File.FileContext);
toText(): string;
}

@@ -6,4 +6,4 @@ "use strict";

class TextFile extends file_1.File.File {
constructor(path, possiblePathInProject) {
super('text', path, possiblePathInProject);
constructor(path, context) {
super('text', path, context);
this.content = '';

@@ -10,0 +10,0 @@ }

@@ -83,2 +83,5 @@ "use strict";

if (!/^CONFLICT/m.test(error.stdout)) {
if (/^error: Your local changes/m.test(error.stderr)) {
throw new Error('Error merging magicspace changes, conflict with local changes');
}
throw new Error('Error merging magicspace changes');

@@ -85,0 +88,0 @@ }

@@ -16,2 +16,2 @@ export declare function unique<T>(values: T[]): T[];

export declare function conservativelyMove(from: string, to: string): boolean;
export declare function getClosetExistingUpperDirectory(path: string): string | undefined;
export declare function removePathExtension(path: string): string;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getClosetExistingUpperDirectory = exports.conservativelyMove = exports.spawnSync = exports.SpawnSyncFailure = exports.uniqueBy = exports.unique = void 0;
exports.removePathExtension = exports.conservativelyMove = exports.spawnSync = exports.SpawnSyncFailure = exports.uniqueBy = exports.unique = void 0;
const tslib_1 = require("tslib");

@@ -72,13 +72,6 @@ const ChildProcess = tslib_1.__importStar(require("child_process"));

exports.conservativelyMove = conservativelyMove;
function getClosetExistingUpperDirectory(path) {
while (!FSExtra.existsSync(path)) {
let upperPath = Path.dirname(path);
if (upperPath === path) {
return undefined;
}
path = upperPath;
}
return path;
function removePathExtension(path) {
return path.slice(0, -Path.extname(path).length);
}
exports.getClosetExistingUpperDirectory = getClosetExistingUpperDirectory;
exports.removePathExtension = removePathExtension;
//# sourceMappingURL=@utils.js.map

@@ -20,1 +20,11 @@ /// <reference types="node" />

}): File.Composable<Buffer, BinaryFileOptions>[];
export interface HandlebarsOptions {
/**
* Path to template, defaults to file sibling to the composable module with
* name `removePathExtension(composableModulePath) + '.hbs'` if not
* specified.
*/
template?: string;
}
export declare function handlebars<TData>(data: TData, options?: HandlebarsOptions): File.Composable<string, TextFileOptions>;
export declare function handlebars<TData>(path: string, data: TData, options?: HandlebarsOptions): File.Composable<string, TextFileOptions>;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.copy = exports.json = exports.text = void 0;
exports.handlebars = exports.copy = exports.json = exports.text = void 0;
const tslib_1 = require("tslib");

@@ -8,2 +8,4 @@ const FS = tslib_1.__importStar(require("fs"));

const FastGlob = tslib_1.__importStar(require("fast-glob"));
const Handlebars = tslib_1.__importStar(require("handlebars"));
const _utils_1 = require("./@utils");
function text(...args) {

@@ -87,2 +89,24 @@ let path;

exports.copy = copy;
function handlebars(...args) {
let path;
let data;
let options;
if (typeof args[0] === 'string') {
[path, data, options = {}] = args;
}
else {
[data, options = {}] = args;
}
return {
type: 'text',
path,
compose(_content, { composableModulePath }) {
var _a;
let templatePath = (_a = options.template) !== null && _a !== void 0 ? _a : `${_utils_1.removePathExtension(composableModulePath)}.hbs`;
let template = FS.readFileSync(templatePath, 'utf8');
return Handlebars.compile(template)(data);
},
};
}
exports.handlebars = handlebars;
//# sourceMappingURL=composables.js.map

@@ -11,3 +11,8 @@ import { File } from './file';

}
export declare type ComposeFunction<TFile extends File<any, any>> = (content: TFile extends File<infer TContent, any> ? TContent : never, file: TFile) => TFile extends File<infer TContent, any> ? TContent : never;
export interface ComposeContext<TFile extends File<any, any>> {
file: TFile;
possibleOutputPath: string;
composableModulePath: string;
}
export declare type ComposeFunction<TFile extends File<any, any>> = (content: TFile extends File<infer TContent, any> ? TContent : never, ComposeContext: ComposeContext<TFile>) => TFile extends File<infer TContent, any> ? Promise<TContent> | TContent : never;
/**

@@ -14,0 +19,0 @@ * A utility function that returns the givin composable as-is.

@@ -6,7 +6,7 @@ /// <reference types="node" />

readonly path: string;
readonly possiblePathInProject: string;
readonly context: FileContext;
abstract content: TContent;
composables: Composable<TContent, TComposeOptions>[];
constructor(type: string, path: string, possiblePathInProject: string);
compose(composable: Composable<TContent, TComposeOptions>): void;
constructor(type: string, path: string, context: FileContext);
compose(composable: Composable<TContent, TComposeOptions>, context: FileComposeContext): Promise<void>;
toText?(): string;

@@ -16,1 +16,7 @@ toBuffer?(): Buffer;

}
export interface FileContext {
possibleOutputPath: string;
}
export interface FileComposeContext {
composableModulePath: string;
}

@@ -8,11 +8,15 @@ "use strict";

class File {
constructor(type, path, possiblePathInProject) {
constructor(type, path, context) {
this.type = type;
this.path = path;
this.possiblePathInProject = possiblePathInProject;
this.context = context;
this.composables = [];
}
compose(composable) {
async compose(composable, context) {
this.composables.push(composable);
this.content = composable.compose(this.content, this);
this.content = await composable.compose(this.content, {
file: this,
...this.context,
...context,
});
}

@@ -24,9 +28,11 @@ async save() {

content = this.toText();
let possiblePathInProject = this.possiblePathInProject;
let Prettier = _prettier_1.getPrettierModule(possiblePathInProject);
let prettierConfigOptions = (_a = (await Prettier.resolveConfig(possiblePathInProject))) !== null && _a !== void 0 ? _a : (await Prettier.resolveConfig(this.path, {
let { possibleOutputPath } = this.context;
let Prettier = _prettier_1.getPrettierModule(possibleOutputPath);
let prettierConfigOptions = (_a = (await Prettier.resolveConfig(possibleOutputPath))) !== null && _a !== void 0 ? _a : (await Prettier.resolveConfig(this.path, {
useCache: false,
}));
if (prettierConfigOptions) {
let { inferredParser } = await Prettier.getFileInfo(possiblePathInProject, { resolveConfig: true });
let { inferredParser } = await Prettier.getFileInfo(possibleOutputPath, {
resolveConfig: true,
});
if (inferredParser) {

@@ -33,0 +39,0 @@ content = Prettier.format(content, {

import { File } from '../file';
import { Context } from './context';
export declare type ComposableModule = File.Composable<unknown, unknown> | File.Composable<unknown, unknown>[] | ComposableModuleFunction;
export declare type ComposableModuleFunction = (options: Magicspace.TemplateOptions, context: Context) => File.Composable<unknown, unknown> | File.Composable<unknown, unknown>[];
export declare type ComposableModuleFunction = (options: Magicspace.TemplateOptions, context: Context) => File.Composable<unknown, unknown> | File.Composable<unknown, unknown>[] extends infer TReturn ? Promise<TReturn> | TReturn : never;

@@ -23,3 +23,5 @@ "use strict";

else {
file = project.createFileObject(path, Path.join(project.dir, Path.relative(this.dir, path)), composable.type);
file = project.createFileObject(path, {
possibleOutputPath: Path.join(project.dir, Path.relative(this.dir, path)),
}, composable.type);
fileMap.set(path, file);

@@ -26,0 +28,0 @@ }

@@ -8,18 +8,6 @@ "use strict";

exports.DEFAULT_FILE_OBJECT_CREATOR_MAP = new Map([
[
undefined,
(path, possiblePathInProject) => new _files_1.TextFile(path, possiblePathInProject),
],
[
'text',
(path, possiblePathInProject) => new _files_1.TextFile(path, possiblePathInProject),
],
[
'binary',
(path, possiblePathInProject) => new _files_1.BinaryFile(path, possiblePathInProject),
],
[
'json',
(path, possiblePathInProject) => new _files_1.JSONFile(path, possiblePathInProject),
],
[undefined, (path, context) => new _files_1.TextFile(path, context)],
['text', (path, context) => new _files_1.TextFile(path, context)],
['binary', (path, context) => new _files_1.BinaryFile(path, context)],
['json', (path, context) => new _files_1.JSONFile(path, context)],
]);

@@ -26,0 +14,0 @@ exports.DEFAULT_EXTENSION_TO_FILE_TYPE_MAP = new Map([

@@ -11,4 +11,4 @@ import { Rename } from '../@git';

constructor(fileObjectCreatorMap: Map<string | undefined, FileObjectCreator>, extensionToFileTypeMap: Map<string, string>, dir: string, _config?: Config.Config | undefined);
initialize({ force, ours, }: ProjectInitializeOptions): Promise<'not-repository-root' | 'already-initialized' | 'working-directory-not-clean' | true>;
update({ force, }: ProjectUpdateOptions): Promise<'not-repository-root' | 'working-directory-not-clean' | 'not-initialized' | 'already-up-to-date' | true>;
initialize({ force, ours, }: ProjectInitializeOptions): Promise<'not-repository-root' | 'already-initialized' | 'merge-in-progress' | 'working-directory-not-clean' | true>;
update({ force, }: ProjectUpdateOptions): Promise<'not-repository-root' | 'merge-in-progress' | 'working-directory-not-clean' | 'not-initialized' | 'already-up-to-date' | true>;
isRepositoryRoot(): boolean;

@@ -19,3 +19,3 @@ isMerging(): boolean;

generate(outputDir: string, options?: Magicspace.TemplateOptions): Promise<void>;
createFileObject(path: string, possiblePathInProject: string, type?: string | undefined): File.File<unknown, unknown>;
createFileObject(path: string, context: File.FileContext, type?: string | undefined): File.File<unknown, unknown>;
assertFileObject(file: File.File<unknown, unknown>, path: string, type: string | undefined): void;

@@ -30,3 +30,3 @@ }

}
export declare type FileObjectCreator = (path: string, possiblePathInProject: string) => File.File<unknown, unknown>;
export declare type FileObjectCreator = (path: string, context: File.FileContext) => File.File<unknown, unknown>;
/**

@@ -33,0 +33,0 @@ * Possible directory rename from and to, relative path.

@@ -32,2 +32,5 @@ "use strict";

}
if (projectGit.isMerging()) {
return 'merge-in-progress';
}
if (!force && !projectGit.isWorkingDirectoryClean()) {

@@ -57,2 +60,5 @@ return 'working-directory-not-clean';

}
if (projectGit.isMerging()) {
return 'merge-in-progress';
}
if (!force && !projectGit.isWorkingDirectoryClean()) {

@@ -104,3 +110,3 @@ return 'working-directory-not-clean';

let module = require(composableModulePath);
let composables = typeof module === 'function' ? module(options, context) : module;
let composables = typeof module === 'function' ? await module(options, context) : module;
if (!Array.isArray(composables)) {

@@ -120,3 +126,3 @@ composables = [composables];

let file = context.ensureFile(path, composable);
file.compose(composable);
await file.compose(composable, { composableModulePath });
}

@@ -126,3 +132,3 @@ }

}
createFileObject(path, possiblePathInProject, type = this.extensionToFileTypeMap.get(Path.dirname(path))) {
createFileObject(path, context, type = this.extensionToFileTypeMap.get(Path.dirname(path))) {
if (!type) {

@@ -135,3 +141,3 @@ throw new Error(`Cannot infer composable file type from path ${JSON.stringify(path)}`);

}
return creator(path, possiblePathInProject);
return creator(path, context);
}

@@ -138,0 +144,0 @@ assertFileObject(file, path, type) {

{
"name": "@magicspace/core",
"version": "0.2.3",
"version": "0.2.4",
"description": "",

@@ -39,3 +39,3 @@ "author": "Chengdu Mufan Technology Co., Ltd.",

},
"gitHead": "49350525408ce412604a810b2f65159eeaaa69e0"
"gitHead": "870c63e74cafbc3973a2aec46582a97b635a2890"
}

@@ -8,4 +8,4 @@ import {File} from '../file';

constructor(path: string, possiblePathInProject: string) {
super('binary', path, possiblePathInProject);
constructor(path: string, context: File.FileContext) {
super('binary', path, context);
}

@@ -12,0 +12,0 @@

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

import {File} from '../file';
import {StructuredFile, StructuredFileOptions} from './structured-file';

@@ -11,4 +13,4 @@

constructor(path: string, possiblePathInProject: string) {
super('json', path, possiblePathInProject);
constructor(path: string, context: File.FileContext) {
super('json', path, context);
}

@@ -15,0 +17,0 @@

@@ -8,4 +8,4 @@ import {File} from '../file';

constructor(path: string, possiblePathInProject: string) {
super('text', path, possiblePathInProject);
constructor(path: string, context: File.FileContext) {
super('text', path, context);
}

@@ -12,0 +12,0 @@

@@ -103,2 +103,8 @@ import * as Path from 'path';

if (!/^CONFLICT/m.test(error.stdout)) {
if (/^error: Your local changes/m.test(error.stderr)) {
throw new Error(
'Error merging magicspace changes, conflict with local changes',
);
}
throw new Error('Error merging magicspace changes');

@@ -105,0 +111,0 @@ }

@@ -86,16 +86,4 @@ import * as ChildProcess from 'child_process';

export function getClosetExistingUpperDirectory(
path: string,
): string | undefined {
while (!FSExtra.existsSync(path)) {
let upperPath = Path.dirname(path);
if (upperPath === path) {
return undefined;
}
path = upperPath;
}
return path;
export function removePathExtension(path: string): string {
return path.slice(0, -Path.extname(path).length);
}

@@ -5,2 +5,3 @@ import * as FS from 'fs';

import * as FastGlob from 'fast-glob';
import * as Handlebars from 'handlebars';

@@ -14,2 +15,3 @@ import {

} from './@files';
import {removePathExtension} from './@utils';
import {File} from './file';

@@ -141,1 +143,46 @@

}
export interface HandlebarsOptions {
/**
* Path to template, defaults to file sibling to the composable module with
* name `removePathExtension(composableModulePath) + '.hbs'` if not
* specified.
*/
template?: string;
}
export function handlebars<TData>(
data: TData,
options?: HandlebarsOptions,
): File.Composable<string, TextFileOptions>;
export function handlebars<TData>(
path: string,
data: TData,
options?: HandlebarsOptions,
): File.Composable<string, TextFileOptions>;
export function handlebars(
...args: any[]
): File.Composable<string, TextFileOptions> {
let path: string | undefined;
let data: unknown;
let options: HandlebarsOptions;
if (typeof args[0] === 'string') {
[path, data, options = {}] = args;
} else {
[data, options = {}] = args;
}
return {
type: 'text',
path,
compose(_content, {composableModulePath}) {
let templatePath =
options.template ?? `${removePathExtension(composableModulePath)}.hbs`;
let template = FS.readFileSync(templatePath, 'utf8');
return Handlebars.compile(template)(data);
},
};
}

@@ -13,6 +13,14 @@ import {File} from './file';

export interface ComposeContext<TFile extends File<any, any>> {
file: TFile;
possibleOutputPath: string;
composableModulePath: string;
}
export type ComposeFunction<TFile extends File<any, any>> = (
content: TFile extends File<infer TContent, any> ? TContent : never,
file: TFile,
) => TFile extends File<infer TContent, any> ? TContent : never;
ComposeContext: ComposeContext<TFile>,
) => TFile extends File<infer TContent, any>
? Promise<TContent> | TContent
: never;

@@ -19,0 +27,0 @@ /**

@@ -16,8 +16,15 @@ import * as FSExtra from 'fs-extra';

readonly path: string,
readonly possiblePathInProject: string,
readonly context: FileContext,
) {}
compose(composable: Composable<TContent, TComposeOptions>): void {
async compose(
composable: Composable<TContent, TComposeOptions>,
context: FileComposeContext,
): Promise<void> {
this.composables.push(composable);
this.content = composable.compose(this.content, this);
this.content = await composable.compose(this.content, {
file: this,
...this.context,
...context,
});
}

@@ -35,8 +42,8 @@

let possiblePathInProject = this.possiblePathInProject;
let {possibleOutputPath} = this.context;
let Prettier = getPrettierModule(possiblePathInProject);
let Prettier = getPrettierModule(possibleOutputPath);
let prettierConfigOptions =
(await Prettier.resolveConfig(possiblePathInProject)) ??
(await Prettier.resolveConfig(possibleOutputPath)) ??
(await Prettier.resolveConfig(this.path, {

@@ -47,6 +54,5 @@ useCache: false,

if (prettierConfigOptions) {
let {inferredParser} = await Prettier.getFileInfo(
possiblePathInProject,
{resolveConfig: true},
);
let {inferredParser} = await Prettier.getFileInfo(possibleOutputPath, {
resolveConfig: true,
});

@@ -71,1 +77,9 @@ if (inferredParser) {

}
export interface FileContext {
possibleOutputPath: string;
}
export interface FileComposeContext {
composableModulePath: string;
}

@@ -13,2 +13,6 @@ import {File} from '../file';

context: Context,
) => File.Composable<unknown, unknown> | File.Composable<unknown, unknown>[];
) =>
| File.Composable<unknown, unknown>
| File.Composable<unknown, unknown>[] extends infer TReturn
? Promise<TReturn> | TReturn
: never;

@@ -31,3 +31,8 @@ import * as Path from 'path';

path,
Path.join(project.dir, Path.relative(this.dir, path)),
{
possibleOutputPath: Path.join(
project.dir,
Path.relative(this.dir, path),
),
},
composable.type,

@@ -34,0 +39,0 @@ );

@@ -10,19 +10,6 @@ import {BinaryFile, JSONFile, TextFile} from '../@files';

>([
[
undefined,
(path, possiblePathInProject) => new TextFile(path, possiblePathInProject),
],
[
'text',
(path, possiblePathInProject) => new TextFile(path, possiblePathInProject),
],
[
'binary',
(path, possiblePathInProject) =>
new BinaryFile(path, possiblePathInProject),
],
[
'json',
(path, possiblePathInProject) => new JSONFile(path, possiblePathInProject),
],
[undefined, (path, context) => new TextFile(path, context)],
['text', (path, context) => new TextFile(path, context)],
['binary', (path, context) => new BinaryFile(path, context)],
['json', (path, context) => new JSONFile(path, context)],
]);

@@ -29,0 +16,0 @@

@@ -36,2 +36,3 @@ import * as Path from 'path';

| 'already-initialized'
| 'merge-in-progress'
| 'working-directory-not-clean'

@@ -50,2 +51,6 @@ | true

if (projectGit.isMerging()) {
return 'merge-in-progress';
}
if (!force && !projectGit.isWorkingDirectoryClean()) {

@@ -82,2 +87,3 @@ return 'working-directory-not-clean';

| 'not-repository-root'
| 'merge-in-progress'
| 'working-directory-not-clean'

@@ -98,2 +104,6 @@ | 'not-initialized'

if (projectGit.isMerging()) {
return 'merge-in-progress';
}
if (!force && !projectGit.isWorkingDirectoryClean()) {

@@ -172,3 +182,3 @@ return 'working-directory-not-clean';

let composables =
typeof module === 'function' ? module(options, context) : module;
typeof module === 'function' ? await module(options, context) : module;

@@ -193,3 +203,3 @@ if (!Array.isArray(composables)) {

file.compose(composable);
await file.compose(composable, {composableModulePath});
}

@@ -203,3 +213,3 @@ }

path: string,
possiblePathInProject: string,
context: File.FileContext,
type = this.extensionToFileTypeMap.get(Path.dirname(path)),

@@ -219,3 +229,3 @@ ): File.File<unknown, unknown> {

return creator(path, possiblePathInProject);
return creator(path, context);
}

@@ -274,3 +284,3 @@

path: string,
possiblePathInProject: string,
context: File.FileContext,
) => File.File<unknown, unknown>;

@@ -277,0 +287,0 @@

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

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

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