Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🆙 Upgrade Jupyter Books in myst init #1223

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions .changeset/thick-fans-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"myst-cli": minor
---

Add Jupyter Book upgrade feature
11 changes: 10 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/myst-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@
"myst-spec": "^0.0.5",
"myst-spec-ext": "^1.4.6",
"myst-templates": "^1.0.19",
"myst-toc": "^0.1.1",
"myst-to-docx": "^1.0.10",
"myst-to-jats": "^1.0.26",
"myst-to-md": "^1.0.11",
"myst-to-tex": "^1.0.29",
"myst-to-typst": "^0.0.17",
"myst-toc": "^0.1.1",
"myst-transforms": "^1.3.17",
"nanoid": "^4.0.0",
"nbtx": "^0.2.3",
Expand All @@ -105,7 +105,8 @@
"vfile": "^5.3.5",
"which": "^3.0.1",
"ws": "^8.9.0",
"xml-js": "^1.6.11"
"xml-js": "^1.6.11",
"zod": "^3.23.8"
},
"devDependencies": {
"@jupyterlab/nbformat": "3.5.2",
Expand Down
1 change: 0 additions & 1 deletion packages/myst-cli/src/build/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export * from './build.js';
export * from './clean.js';
export * from './docx/index.js';
export * from './init.js';
export * from './pdf/index.js';
export * from './site/index.js';
export * from './tex/index.js';
Expand Down
1 change: 0 additions & 1 deletion packages/myst-cli/src/build/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ export * from './getFileContent.js';
export * from './localArticleExport.js';
export * from './resolveAndLogErrors.js';
export * from './bibtex.js';
export * from './github.js';
1 change: 1 addition & 0 deletions packages/myst-cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './build/index.js';
export * from './cli/index.js';
export * from './config.js';
export * from './init/index.js';
export * from './frontmatter.js';
export * from './plugins.js';
export * from './process/index.js';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import path from 'node:path';
import inquirer from 'inquirer';
import chalk from 'chalk';
import type { ISession } from 'myst-cli-utils';
import { makeExecutable, writeFileToFolder } from 'myst-cli-utils';
import { getGithubUrl } from '../utils/github.js';
import { writeFileToFolder } from 'myst-cli-utils';
import { getGithubUrl } from '../../utils/github.js';
import { checkFolderIsGit, checkAtGitRoot } from '../../utils/git.js';

function createGithubPagesAction({
defaultBranch = 'main',
Expand Down Expand Up @@ -97,24 +98,6 @@ jobs:
`;
}

async function checkFolderIsGit(): Promise<boolean> {
try {
await makeExecutable('git status', null)();
return true;
} catch (error) {
return false;
}
}

async function checkAtGitRoot(): Promise<boolean> {
try {
fs.readdirSync('.git');
return true;
} catch (error) {
return false;
}
}

async function prelimGitChecks(session: ISession): Promise<string | undefined> {
const inGitRepo = await checkFolderIsGit();
if (!inGitRepo) {
Expand Down
1 change: 1 addition & 0 deletions packages/myst-cli/src/init/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './init.js';
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import fs from 'node:fs';
import path from 'node:path';
import yaml from 'js-yaml';
import { v4 as uuid } from 'uuid';
import inquirer from 'inquirer';
import chalk from 'chalk';
import { defaultConfigFile, loadConfig, writeConfigs } from '../config.js';
import { loadProjectFromDisk } from '../project/load.js';
import { selectors } from '../store/index.js';
import type { ISession } from '../session/types.js';
import inquirer from 'inquirer';
import chalk from 'chalk';
import { startServer } from './site/start.js';
import { startServer } from '../build/site/start.js';
import { githubCurvenoteAction, githubPagesAction } from './gh-actions/index.js';
import { getGithubUrl } from './utils/github.js';
import { getGithubUrl } from '../utils/github.js';
import { upgradeJupyterBook } from './jupyter-book/upgrade.js';
import { fsExists } from '../utils/fsExists.js';

const VERSION_CONFIG = '# See docs at: https://mystmd.org/guide/frontmatter\nversion: 1\n';

Expand Down Expand Up @@ -93,24 +95,35 @@ export async function init(session: ISession, opts: InitOptions) {
await writeConfigs(session, '.', { siteConfig, projectConfig });
}
} else {
// If no config is present, write it explicitly to include comments.
const configFile = defaultConfigFile(session, '.');
let configData: string;
let configDoc: string;
if (site && !project) {
configData = `${VERSION_CONFIG}${SITE_CONFIG}`;
configDoc = 'site';
} else if (project && !site) {
configData = `${VERSION_CONFIG}${createProjectConfig({ github })}`;
configDoc = 'project';
} else {
configData = `${VERSION_CONFIG}${createProjectConfig({ github })}${SITE_CONFIG}`;
configDoc = 'project and site';
// Is this a Jupyter Book?
if (await fsExists('_config.yml')) {
const configFile = defaultConfigFile(session, '.');
session.log.info(
`📘 Found a legacy Jupyter Book, writing new config file: ${chalk.blue(path.resolve(configFile))}`,
);
await upgradeJupyterBook(session, configFile);
}
// Otherwise, write some default configs
else {
// If no config is present, write it explicitly to include comments.
const configFile = defaultConfigFile(session, '.');
let configData: string;
let configDoc: string;
if (site && !project) {
configData = `${VERSION_CONFIG}${SITE_CONFIG}`;
configDoc = 'site';
} else if (project && !site) {
configData = `${VERSION_CONFIG}${createProjectConfig({ github })}`;
configDoc = 'project';
} else {
configData = `${VERSION_CONFIG}${createProjectConfig({ github })}${SITE_CONFIG}`;
configDoc = 'project and site';
}
session.log.info(
`💾 Writing new ${configDoc} config file: ${chalk.blue(path.resolve(configFile))}`,
);
fs.writeFileSync(configFile, configData);
}
session.log.info(
`💾 Writing new ${configDoc} config file: ${chalk.blue(path.resolve(configFile))}`,
);
fs.writeFileSync(configFile, configData);
}
if (writeTOC) {
await loadConfig(session, '.');
Expand Down