Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Feature/version#8

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ShMcK merged 5 commits intomasterfromfeature/version
May 31, 2020
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
build/parse refactor
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
  • Loading branch information
@ShMcK
ShMcK committedMay 31, 2020
commit740c1cf81c68615adbf2e5e503d0b4ce58b8273a
70 changes: 4 additions & 66 deletionssrc/build.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,80 +5,18 @@ import * as fs from "fs";
import * as util from "util";
import { parse } from "./utils/parse";
import { getArg } from "./utils/args";
import { getCommits } from "./utils/commits";
import { getCommits, CommitLogObject } from "./utils/commits";
import * as T from "../typings/tutorial";

const write = util.promisify(fs.writeFile);
const read = util.promisify(fs.readFile);

// import not working

const workingDir = "tmp";

function rmDir(dir: string, rmSelf = false) {
try {
let files;
rmSelf = rmSelf === undefined ? true : rmSelf;

try {
files = fs.readdirSync(dir);
} catch (e) {
console.log(`Sorry, directory '${dir}' doesn't exist.`);
return;
}

if (files.length > 0) {
files.forEach(function (filePath: string) {
if (fs.statSync(path.join(dir, filePath)).isDirectory()) {
rmDir(path.join(dir, filePath));
} else {
fs.unlinkSync(path.join(dir, filePath));
}
});
}

if (rmSelf) {
// check if user want to delete the directory ir just the files in this directory
fs.rmdirSync(dir);
}
} catch (error) {
return error;
}
}

async function cleanupFiles(workingDir: string) {
try {
const gitModule = simpleGit(process.cwd());

await gitModule.subModule(["deinit", "-f", workingDir]);
await gitModule.rm(workingDir);
await gitModule.reset(["HEAD"]);
rmDir(path.join(process.cwd(), ".git", "modules", workingDir));
rmDir(workingDir);
} catch (error) {
return error;
}
}

export type BuildConfigOptions = {
text: string; // text document from markdown
config: T.Tutorial; // yaml config file converted to json
commits:{ [key: string]: string[] };
commits:CommitLogObject; // an object of tutorial positions with a list of commit hashes
};

async function generateConfig({ text, config, commits }: BuildConfigOptions) {
const tutorial = parse(text, config);

// const isValid = validate(tutorial);

// if (!isValid) {
// console.log(JSON.stringify(validate.errors, null, 2));
// return;
// }

return tutorial;
}

type BuildArgs = {
dir: string;
markdown: string;
Expand DownExpand Up@@ -122,10 +60,10 @@ async function build(args: string[]) {

const config = yamlParser.load(_yaml);

const commits = getCommits(config.config.repo.branch);
const commits: CommitLogObject = await getCommits(config.config.repo.branch);

// Otherwise, continue with the other options
const tutorial: T.Tutorial = awaitgenerateConfig({
const tutorial: T.Tutorial = awaitparse({
text: _markdown,
config,
commits,
Expand Down
163 changes: 58 additions & 105 deletionssrc/utils/commits.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,69 @@
import * as fs from "fs";
import util from "util";
import * as path from "path";
import gitP, { SimpleGit, StatusResult } from "simple-git/promise";
import gitP, { SimpleGit } from "simple-git/promise";
import * as T from "../../typings/tutorial";

export async function getCommits() {
const git: SimpleGit = gitP(process.cwd());
const mkdir = util.promisify(fs.mkdir);
const exists = util.promisify(fs.exists);
const rmdir = util.promisify(fs.rmdir);

const isRepo = await git.checkIsRepo();
type GetCommitOptions = {
localDir: string;
codeBranch: string;
};

export type CommitLogObject = { [position: string]: string[] };

const tmpDirectory = "tmp";
const localPath = path.join(process.cwd(), tmpDirectory);
export async function getCommits({
localDir,
codeBranch,
}: GetCommitOptions): Promise<CommitLogObject> {
const git: SimpleGit = gitP(localDir);

const isRepo = await git.checkIsRepo();

if (!isRepo) {
throw new Error("No git repo provided");
}

// if (isRepo) {
// await gitTest.submoduleAdd(repo, workingDir);

// isSubModule = true;
// } else {
// await gitTest.clone(repo, localPath);
// }

// await git.fetch();

// // checkout the branch to load tutorialuration and content branch
// await git.checkout(setupBranch);

// // Checkout the code branches
// await git.checkout(codeBranch);

// // Load all logs
// const logs = await git.log();

// // Filter relevant logs
// const parts = new Set();
const tmpDir = path.join(localDir, ".tmp");

// for (const commit of logs.all) {
// const matches = commit.message.match(
// /^(?<stepId>(?<levelId>L\d+)S\d+)(?<stepType>[QA])?/
// );

// if (matches && !parts.has(matches[0])) {
// // Uses a set to make sure only the latest commit is proccessed
// parts.add(matches[0]);

// // Add the content and git hash to the tutorial
// if (matches.groups.stepId) {
// // If it's a step: add the content and the setup/solution hashes depending on the type
// const level: T.Level | null =
// tutorial.levels.find(
// (level: T.Level) => level.id === matches.groups.levelId
// ) || null;
// if (!level) {
// console.log(`Level ${matches.groups.levelId} not found`);
// } else {
// const theStep: T.Step | null =
// level.steps.find(
// (step: T.Step) => step.id === matches.groups.stepId
// ) || null;

// if (!theStep) {
// console.log(`Step ${matches.groups.stepId} not found`);
// } else {
// if (matches.groups.stepType === "Q") {
// theStep.setup.commits.push(commit.hash.substr(0, 7));
// } else if (
// matches.groups.stepType === "A" &&
// theStep.solution &&
// theStep.solution.commits
// ) {
// theStep.solution.commits.push(commit.hash.substr(0, 7));
// }
// }
// }
// } else {
// // If it's level: add the commit hash (if the level has the commit key) and the content to the tutorial
// const theLevel: T.Level | null =
// tutorial.levels.find(
// (level: T.Level) => level.id === matches.groups.levelId
// ) || null;

// if (!theLevel) {
// console.log(`Level ${matches.groups.levelId} not found`);
// } else {
// if (_.has(theLevel, "tutorial.commits")) {
// if (theLevel.setup) {
// theLevel.setup.commits.push(commit.hash.substr(0, 7));
// }
// }
// }
// }
// }
// }

// // cleanup the submodules
// if (!isLocal) {
// let cleanupErr;

// if (isSubModule) {
// cleanupErr = await cleanupFiles(workingDir);
// } else {
// cleanupErr = rmDir(path.join(process.cwd(), workingDir));
// }

// if (cleanupErr) {
// console.log(
// `Error when deleting temporary files on ${
// isSubModule ? "module" : "folder"
// } ${workingDir}.`
// );
// }
// }
const tmpDirExists = await exists(tmpDir);
if (tmpDirExists) {
await rmdir(tmpDir, { recursive: true });
}
await mkdir(tmpDir);
const tempGit = gitP(tmpDir);
await tempGit.clone(localDir, tmpDir);

// Checkout the code branches
await git.checkout(codeBranch);

// Load all logs
const logs = await git.log();

// Filter relevant logs
const commits: CommitLogObject = {};

for (const commit of logs.all) {
const matches = commit.message.match(
/^(?<stepId>(?<levelId>L\d+)(S\d+))(?<stepType>[QA])?/
);

if (matches && matches.length) {
// Use an object of commit arrays to collect all commits
const position = matches[0];
if (!commits[position]) {
// does not exist, create the list
commits[position] = [commit.hash];
} else {
// add to the list
commits[position].push(commit.hash);
}
}
}
// cleanup the tmp directory
await rmdir(tmpDir, { recursive: true });
return commits;
}

getCommits();
77 changes: 68 additions & 9 deletionssrc/utils/parse.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
import * as _ from "lodash";
import { CommitLogObject } from "./commits";
import * as T from "../../typings/tutorial";

type TutorialFrame = {
Expand DownExpand Up@@ -91,17 +92,23 @@ export function parseMdContent(md: string): TutorialFrame | never {
return sections;
}

export function parse(_content: string, _config: string): T.Tutorial {
const mdContent: TutorialFrame = parseMdContent(_content);
// Parse tutorial to JSON
const tutorial: T.Tutorial = yamlParser.load(_config);
type ParseParams = {
text: string;
config: T.Tutorial;
commits: CommitLogObject;
};

export function parse(params: ParseParams): T.Tutorial {
const parsed = { ...params.config };

const mdContent: TutorialFrame = parseMdContent(params.text);

// Add the summary to the tutorial file
tutorial["summary"] = mdContent.summary;
parsed["summary"] = mdContent.summary;

// merge content and tutorial
if (tutorial.levels) {
tutorial.levels.forEach((level: T.Level) => {
if (parsed.levels) {
parsed.levels.forEach((level: T.Level, levelIndex: number) => {
const levelContent = mdContent[level.id];
if (!levelContent) {
console.log(`Markdown content not found for ${level.id}`);
Expand All@@ -110,12 +117,64 @@ export function parse(_content: string, _config: string): T.Tutorial {
const { steps, ...content } = levelContent;

if (steps) {
steps.forEach((step: T.Step) => _.merge(step, steps[step.id]));
steps.forEach((step: T.Step, stepIndex: number) => {
return _.merge(step, steps[step.id]);
});
}

_.merge(level, content);
});
}

returntutorial;
returnparsed;
}

/*
// Add the content and git hash to the tutorial
if (matches.groups.stepId) {
// If it's a step: add the content and the setup/solution hashes depending on the type
const level: T.Level | null =
tutorial.levels.find(
(level: T.Level) => level.id === matches.groups.levelId
) || null;
if (!level) {
console.log(`Level ${matches.groups.levelId} not found`);
} else {
const theStep: T.Step | null =
level.steps.find(
(step: T.Step) => step.id === matches.groups.stepId
) || null;

if (!theStep) {
console.log(`Step ${matches.groups.stepId} not found`);
} else {
if (matches.groups.stepType === "Q") {
theStep.setup.commits.push(commit.hash.substr(0, 7));
} else if (
matches.groups.stepType === "A" &&
theStep.solution &&
theStep.solution.commits
) {
theStep.solution.commits.push(commit.hash.substr(0, 7));
}
}
}
} else {
// If it's level: add the commit hash (if the level has the commit key) and the content to the tutorial
const theLevel: T.Level | null =
tutorial.levels.find(
(level: T.Level) => level.id === matches.groups.levelId
) || null;

if (!theLevel) {
console.log(`Level ${matches.groups.levelId} not found`);
} else {
if (_.has(theLevel, "tutorial.commits")) {
if (theLevel.setup) {
theLevel.setup.commits.push(commit.hash.substr(0, 7));
}
}
}
}
}
*/

[8]ページ先頭

©2009-2025 Movatter.jp