@mokr/core
Advanced tools
Comparing version 0.4.4 to 0.5.0
@@ -9,2 +9,3 @@ export * from "./directory.js"; | ||
export * from "./plugin.js"; | ||
export * from "./repo.js"; | ||
export * from "./template.js"; | ||
@@ -11,0 +12,0 @@ export * from "./utils/index.js"; |
import { join } from "node:path"; | ||
import { isReadableAndWritableDirectory } from "./directory.js"; | ||
import { hasPackage, readPackage, writePackage } from "./package.js"; | ||
import { enqueueInstallDependency, initYarn } from "./yarn.js"; | ||
import { readPackage } from "./package.js"; | ||
import { createRepo, DEFAULT_LICENSE, isRepo, } from "./repo.js"; | ||
import { addYarnPlugin } from "./yarn.js"; | ||
export const DEFAULT_SCOPED = true; | ||
export const DEFAULT_LICENSE = "MIT"; | ||
export const DEFAULT_WORKSPACES_DIRECTORY = "packages"; | ||
export async function createMonorepo({ directory, scoped = DEFAULT_SCOPED, license = DEFAULT_LICENSE, workspacesDirectory = DEFAULT_WORKSPACES_DIRECTORY, }) { | ||
if (await isReadableAndWritableDirectory({ directory })) { | ||
throw new Error(`${directory} already exists`); | ||
} | ||
await initYarn({ directory }); | ||
await writePackage({ | ||
await createRepo({ | ||
directory, | ||
data: { | ||
license, | ||
license, | ||
additionalPackageOptions: { | ||
private: true, | ||
workspaces: [`${workspacesDirectory}/*`], | ||
moker: { | ||
scoped, | ||
plugins: [], | ||
}, | ||
scripts: { | ||
build: "echo 'not implemented'", | ||
test: "echo 'not implemented'", | ||
}, | ||
}, | ||
}); | ||
enqueueInstallDependency({ | ||
directory, | ||
identifier: "moker", | ||
dev: true, | ||
}); | ||
await addYarnPlugin({ directory, name: "workspace-tools" }); | ||
} | ||
export async function isMonorepo({ directory }) { | ||
if (!(await hasPackage({ directory }))) { | ||
if (!(await isRepo({ directory }))) { | ||
return false; | ||
@@ -37,0 +24,0 @@ } |
import { isMonorepo } from "./monorepo.js"; | ||
import { readPackage, updatePackage, writePackage } from "./package.js"; | ||
import { isRepo } from "./repo.js"; | ||
import { toCamelCase } from "./utils/string.js"; | ||
import { isWorkspace } from "./workspace.js"; | ||
export var PluginType; | ||
(function (PluginType) { | ||
PluginType["Repo"] = "repo"; | ||
PluginType["RepoOrWorkspace"] = "repoOrWorkspace"; | ||
PluginType["Monorepo"] = "monorepo"; | ||
PluginType["Workspace"] = "workspace"; | ||
PluginType["Any"] = "any"; | ||
})(PluginType || (PluginType = {})); | ||
@@ -48,15 +51,32 @@ const CORE_PLUGINS = [ | ||
} | ||
// Monorepo level? | ||
if (await isMonorepo({ directory })) { | ||
if (plugin.type === "workspace") { | ||
throw new Error(`Plugin ${name} can only be used at workspace level`); | ||
} | ||
} | ||
else { | ||
if (plugin.type === "monorepo") { | ||
throw new Error(`Plugin ${name} can only be used at monorepo level`); | ||
} | ||
} | ||
await validateType({ directory, type: plugin.type }); | ||
return plugin; | ||
} | ||
export async function validateType({ directory, type, }) { | ||
const repo = await isRepo({ directory }); | ||
const monorepo = await isMonorepo({ directory }); | ||
const workspace = await isWorkspace({ directory }); | ||
switch (type) { | ||
case PluginType.Repo: | ||
if (!repo) { | ||
throw new Error(`Plugin can only be used at repo level`); | ||
} | ||
break; | ||
case PluginType.RepoOrWorkspace: | ||
if (!repo && !workspace) { | ||
throw new Error(`Plugin can only be used at repo or workspace level`); | ||
} | ||
break; | ||
case PluginType.Monorepo: | ||
if (!monorepo) { | ||
throw new Error(`Plugin can only be used at monorepo level`); | ||
} | ||
break; | ||
case PluginType.Workspace: | ||
if (!workspace) { | ||
throw new Error(`Plugin can only be used at workspace level`); | ||
} | ||
break; | ||
} | ||
} | ||
export async function installPlugin({ directory, name }) { | ||
@@ -63,0 +83,0 @@ if (await hasPlugin({ directory, name })) { |
@@ -1,9 +0,3 @@ | ||
import { isMonorepo } from "./monorepo.js"; | ||
import { validateType } from "./plugin.js"; | ||
import { toCamelCase } from "./utils/string.js"; | ||
export var TemplateType; | ||
(function (TemplateType) { | ||
TemplateType["Monorepo"] = "monorepo"; | ||
TemplateType["Workspace"] = "workspace"; | ||
TemplateType["Any"] = "any"; | ||
})(TemplateType || (TemplateType = {})); | ||
const CORE_TEMPLATES = ["common", "lib", "cra", "bandersnatch", "express"]; | ||
@@ -31,13 +25,3 @@ export function isTemplate(template) { | ||
} | ||
// Monorepo level? | ||
if (await isMonorepo({ directory })) { | ||
if (template.type === "workspace") { | ||
throw new Error(`Template ${name} can only be used at workspace level`); | ||
} | ||
} | ||
else { | ||
if (template.type === "monorepo") { | ||
throw new Error(`Template ${name} can only be used at monorepo level`); | ||
} | ||
} | ||
await validateType({ directory, type: template.type }); | ||
return template; | ||
@@ -44,0 +28,0 @@ } |
@@ -6,1 +6,7 @@ export function toCamelCase(str) { | ||
} | ||
export function toSnakeCase(str) { | ||
return str | ||
.replace(/([a-z])([A-Z])/g, "$1_$2") | ||
.replace(/[\s-]+/g, "_") | ||
.toLowerCase(); | ||
} |
@@ -1,7 +0,7 @@ | ||
import { dirname, join } from "node:path"; | ||
import { basename, dirname, join } from "node:path"; | ||
import { pkgUp } from "pkg-up"; | ||
import { isReadableAndWritableDirectory } from "./directory.js"; | ||
import { writeFile } from "./file.js"; | ||
import { getScoped, getWorkspacesDirectory } from "./monorepo.js"; | ||
import { readPackage, writePackage } from "./package.js"; | ||
import { getScoped, getWorkspacesDirectory, isMonorepo } from "./monorepo.js"; | ||
import { hasPackage, readPackage, writePackage } from "./package.js"; | ||
const DEFAULT_INITIAL_VERSION = "0.0.0"; | ||
@@ -37,14 +37,29 @@ export async function addWorkspace({ directory, name, }) { | ||
}); | ||
await writeReadme({ directory, name: packageName }); | ||
return workspaceDirectory; | ||
} | ||
export async function writeReadme({ directory, name, }) { | ||
await writeFile({ | ||
path: join(workspaceDirectory, "README.md"), | ||
contents: `# ${packageName}`, | ||
path: join(directory, "README.md"), | ||
contents: `# ${name ?? basename(directory)}`, | ||
}); | ||
return workspaceDirectory; | ||
} | ||
export async function isWorkspace({ directory }) { | ||
if (!(await hasPackage({ directory }))) { | ||
return false; | ||
} | ||
if (await getMonorepoDirectory({ directory })) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
export async function getMonorepoDirectory({ directory }) { | ||
const path = await pkgUp({ cwd: dirname(directory) }); | ||
if (path) { | ||
return dirname(path); | ||
if (!path) { | ||
return; | ||
} | ||
return; | ||
const parentPackageDirectory = dirname(path); | ||
return (await isMonorepo({ directory: parentPackageDirectory })) | ||
? parentPackageDirectory | ||
: undefined; | ||
} |
@@ -6,2 +6,6 @@ import { createDirectory } from "./directory.js"; | ||
const GITIGNORE_LINES = [ | ||
"# node", | ||
"node_modules", | ||
"", | ||
"# yarn", | ||
".pnp.*", | ||
@@ -20,9 +24,15 @@ ".yarn/*", | ||
}; | ||
export async function initYarn({ directory }) { | ||
export async function initYarnExistingRepo({ directory }) { | ||
await exec("yarn", ["set", "version", "latest"], { cwd: directory }); | ||
await initYarn({ directory }); | ||
} | ||
export async function initYarnNewRepo({ directory }) { | ||
await createDirectory({ directory }); | ||
await exec("yarn", ["init", "-2"], { cwd: directory }); | ||
await initYarn({ directory }); | ||
} | ||
async function initYarn({ directory }) { | ||
await writeYarnrc({ directory, data: { nodeLinker: "node-modules" } }); | ||
await writeGitignore({ directory, lines: GITIGNORE_LINES, append: false }); | ||
await addYarnPlugin({ directory, name: "interactive-tools" }); | ||
await addYarnPlugin({ directory, name: "workspace-tools" }); | ||
} | ||
@@ -29,0 +39,0 @@ export async function addYarnPlugin({ directory, name, }) { |
{ | ||
"name": "@mokr/core", | ||
"version": "0.4.4", | ||
"version": "0.5.0", | ||
"description": "Core moker functions", | ||
@@ -19,8 +19,8 @@ "repository": "https://github.com/hongaar/moker", | ||
"scripts": { | ||
"prepublishOnly": "yarn build", | ||
"prepublish": "yarn build", | ||
"clean": "rm -rf dist && rm -rf types", | ||
"build": "yarn clean && tsc", | ||
"build:watch": "yarn build && tsc --watch", | ||
"test": "jest", | ||
"watch:build": "tsc --watch", | ||
"watch:test": "jest --watch" | ||
"test:watch": "jest --watch" | ||
}, | ||
@@ -27,0 +27,0 @@ "dependencies": { |
@@ -9,2 +9,3 @@ export * from "./directory.js"; | ||
export * from "./plugin.js"; | ||
export * from "./repo.js"; | ||
export * from "./template.js"; | ||
@@ -11,0 +12,0 @@ export * from "./utils/index.js"; |
import { Package } from "./package.js"; | ||
import { CreateRepoOptions } from "./repo.js"; | ||
export declare const DEFAULT_SCOPED = true; | ||
export declare const DEFAULT_LICENSE = "MIT"; | ||
export declare const DEFAULT_WORKSPACES_DIRECTORY = "packages"; | ||
@@ -14,6 +14,4 @@ export type MonorepoPackage = Package & { | ||
}; | ||
type CreateMonorepoOptions = DirOption & { | ||
export type CreateMonorepoOptions = DirOption & CreateRepoOptions & { | ||
scoped?: boolean; | ||
license?: string; | ||
initialVersion?: string; | ||
workspacesDirectory?: string; | ||
@@ -20,0 +18,0 @@ }; |
@@ -5,8 +5,9 @@ export type PluginArgs = { | ||
export declare enum PluginType { | ||
Repo = "repo", | ||
RepoOrWorkspace = "repoOrWorkspace", | ||
Monorepo = "monorepo", | ||
Workspace = "workspace", | ||
Any = "any" | ||
Workspace = "workspace" | ||
} | ||
export type Plugin = { | ||
type: string | PluginType; | ||
type: PluginType; | ||
install: (args: PluginArgs) => Promise<void>; | ||
@@ -16,4 +17,6 @@ remove: (args: PluginArgs) => Promise<void>; | ||
}; | ||
type PluginOptions = { | ||
type DirOption = { | ||
directory: string; | ||
}; | ||
type PluginOptions = DirOption & { | ||
name: string; | ||
@@ -23,2 +26,5 @@ }; | ||
export declare function importPlugin({ directory, name }: PluginOptions): Promise<Plugin>; | ||
export declare function validateType({ directory, type, }: DirOption & { | ||
type: PluginType; | ||
}): Promise<void>; | ||
export declare function installPlugin({ directory, name }: PluginOptions): Promise<void>; | ||
@@ -25,0 +31,0 @@ export declare function removePlugin({ directory, name }: PluginOptions): Promise<void>; |
@@ -0,11 +1,7 @@ | ||
import { PluginType } from "./plugin.js"; | ||
export type TemplateArgs = { | ||
directory: string; | ||
}; | ||
export declare enum TemplateType { | ||
Monorepo = "monorepo", | ||
Workspace = "workspace", | ||
Any = "any" | ||
} | ||
export type Template = { | ||
type: string | TemplateType; | ||
type: PluginType; | ||
apply: (args: TemplateArgs) => Promise<void>; | ||
@@ -12,0 +8,0 @@ }; |
export declare function toCamelCase(str: string): string; | ||
export declare function toSnakeCase(str: string): string; | ||
//# sourceMappingURL=string.d.ts.map |
@@ -7,4 +7,8 @@ type DirOption = { | ||
}): Promise<string>; | ||
export declare function writeReadme({ directory, name, }: DirOption & { | ||
name?: string; | ||
}): Promise<void>; | ||
export declare function isWorkspace({ directory }: DirOption): Promise<boolean>; | ||
export declare function getMonorepoDirectory({ directory }: DirOption): Promise<string | undefined>; | ||
export {}; | ||
//# sourceMappingURL=workspace.d.ts.map |
type DirOption = { | ||
directory: string; | ||
}; | ||
export declare function initYarn({ directory }: DirOption): Promise<void>; | ||
export declare function initYarnExistingRepo({ directory }: DirOption): Promise<void>; | ||
export declare function initYarnNewRepo({ directory }: DirOption): Promise<void>; | ||
export declare function addYarnPlugin({ directory, name, }: { | ||
@@ -6,0 +7,0 @@ directory: string; |
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
52001
62
1108