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

Commitecb6e5e

Browse files
authored
Merge pull request#8 from coderoad/feature/version
Feature/version
2 parentsd53c378 +a7d615b commitecb6e5e

File tree

11 files changed

+439
-408
lines changed

11 files changed

+439
-408
lines changed

‎package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name":"@coderoad/cli",
3-
"version":"0.0.3",
3+
"version":"0.0.4",
44
"description":"A CLI to build the configuration file for Coderoad Tutorials",
55
"main":"src/main.js",
66
"bin": {

‎src/build.ts

Lines changed: 57 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -2,197 +2,80 @@ import * as yamlParser from "js-yaml";
22
import*aspathfrom"path";
33
import*as_from"lodash";
44
import*asfsfrom"fs";
5-
import*asTfrom"../typings/tutorial";
5+
import*asutilfrom"util";
66
import{parse}from"./utils/parse";
7-
// import validate from './validator';
8-
9-
// import not working
10-
constsimpleGit=require("simple-git/promise");
11-
12-
constworkingDir="tmp";
13-
14-
functionrmDir(dir:string,rmSelf=false){
15-
try{
16-
letfiles;
17-
rmSelf=rmSelf===undefined ?true :rmSelf;
18-
19-
try{
20-
files=fs.readdirSync(dir);
21-
}catch(e){
22-
console.log(`Sorry, directory '${dir}' doesn't exist.`);
23-
return;
24-
}
25-
26-
if(files.length>0){
27-
files.forEach(function(filePath:string){
28-
if(fs.statSync(path.join(dir,filePath)).isDirectory()){
29-
rmDir(path.join(dir,filePath));
30-
}else{
31-
fs.unlinkSync(path.join(dir,filePath));
32-
}
33-
});
34-
}
35-
36-
if(rmSelf){
37-
// check if user want to delete the directory ir just the files in this directory
38-
fs.rmdirSync(dir);
39-
}
40-
}catch(error){
41-
returnerror;
42-
}
43-
}
7+
import{getArg}from"./utils/args";
8+
import{getCommits,CommitLogObject}from"./utils/commits";
9+
import*asTfrom"../typings/tutorial";
4410

45-
asyncfunctioncleanupFiles(workingDir:string){
46-
try{
47-
constgitModule=simpleGit(process.cwd());
11+
constwrite=util.promisify(fs.writeFile);
12+
constread=util.promisify(fs.readFile);
4813

49-
awaitgitModule.subModule(["deinit","-f",workingDir]);
50-
awaitgitModule.rm(workingDir);
51-
awaitgitModule.reset(["HEAD"]);
52-
rmDir(path.join(process.cwd(),".git","modules",workingDir));
53-
rmDir(workingDir);
54-
}catch(error){
55-
returnerror;
56-
}
57-
}
14+
exporttypeBuildConfigOptions={
15+
text:string;// text document from markdown
16+
config:T.Tutorial;// yaml config file converted to json
17+
commits:CommitLogObject;// an object of tutorial positions with a list of commit hashes
18+
};
5819

59-
exporttypeBuildOptions={
60-
repo:string;// Git url to the repo. It should finish with .git
61-
codeBranch:string;// The branch containing the tutorial code
62-
setupBranch:string;// The branch containing the tutorialuration files
63-
isLocal:boolean;// define if the repo is local or remote
20+
typeBuildArgs={
21+
dir:string;
22+
markdown:string;
23+
yaml:string;
6424
output:string;
6525
};
6626

67-
asyncfunctionbuild({ repo, codeBranch, setupBranch, isLocal}:BuildOptions){
68-
letgit:any;
69-
letisSubModule=false;
70-
letlocalPath:string;
71-
72-
if(isLocal){
73-
git=simpleGit(repo);
74-
localPath=repo;
75-
}else{
76-
constgitTest=simpleGit(process.cwd());
77-
constisRepo=awaitgitTest.checkIsRepo();
78-
localPath=path.join(process.cwd(),workingDir);
79-
80-
if(isRepo){
81-
awaitgitTest.submoduleAdd(repo,workingDir);
82-
83-
isSubModule=true;
84-
}else{
85-
awaitgitTest.clone(repo,localPath);
86-
}
87-
88-
git=simpleGit(localPath);
89-
}
90-
91-
awaitgit.fetch();
92-
93-
// checkout the branch to load tutorialuration and content branch
94-
awaitgit.checkout(setupBranch);
95-
96-
// Load files
97-
const_content=fs.readFileSync(path.join(localPath,"TUTORIAL.md"),"utf8");
98-
let_config=fs.readFileSync(path.join(localPath,"coderoad.yaml"),"utf8");
99-
100-
consttutorial=parse(_content,_config);
101-
102-
// Checkout the code branches
103-
awaitgit.checkout(codeBranch);
104-
105-
// Load all logs
106-
constlogs=awaitgit.log();
27+
constparseArgs=(args:string[]):BuildArgs=>{
28+
// default .
29+
constdir=args[0]||".";
30+
// -o --output - default coderoad.json
31+
constoutput=
32+
getArg(args,{name:"output",alias:"o"})||"coderoad.json";
33+
// -m --markdown - default TUTORIAL.md
34+
constmarkdown=
35+
getArg(args,{name:"markdown",alias:"m"})||"TUTORIAL.md";
36+
// -y --yaml - default coderoad-config.yml
37+
constyaml=
38+
getArg(args,{name:"coderoad-config.yml",alias:"y"})||
39+
"coderoad-config.yml";
40+
41+
return{
42+
dir,
43+
output,
44+
markdown,
45+
yaml,
46+
};
47+
};
10748

108-
// Filter relevant logs
109-
constparts=newSet();
49+
asyncfunctionbuild(args:string[]){
50+
constoptions=parseArgs(args);
11051

111-
for(constcommitoflogs.all){
112-
constmatches=commit.message.match(
113-
/^(?<stepId>(?<levelId>L\d+)S\d+)(?<stepType>[QA])?/
114-
);
52+
// path to run build from
53+
constlocalPath=path.join(process.cwd(),options.dir);
11554

116-
if(matches&&!parts.has(matches[0])){
117-
// Uses a set to make sure only the latest commit is proccessed
118-
parts.add(matches[0]);
55+
// load files
56+
const[_markdown,_yaml]=awaitPromise.all([
57+
read(path.join(localPath,options.markdown),"utf8"),
58+
read(path.join(localPath,options.yaml),"utf8"),
59+
]);
11960

120-
// Add the content and git hash to the tutorial
121-
if(matches.groups.stepId){
122-
// If it's a step: add the content and the setup/solution hashes depending on the type
123-
constlevel:T.Level|null=
124-
tutorial.levels.find(
125-
(level:T.Level)=>level.id===matches.groups.levelId
126-
)||null;
127-
if(!level){
128-
console.log(`Level${matches.groups.levelId} not found`);
129-
}else{
130-
consttheStep:T.Step|null=
131-
level.steps.find(
132-
(step:T.Step)=>step.id===matches.groups.stepId
133-
)||null;
61+
constconfig=yamlParser.load(_yaml);
13462

135-
if(!theStep){
136-
console.log(`Step${matches.groups.stepId} not found`);
137-
}else{
138-
if(matches.groups.stepType==="Q"){
139-
theStep.setup.commits.push(commit.hash.substr(0,7));
140-
}elseif(
141-
matches.groups.stepType==="A"&&
142-
theStep.solution&&
143-
theStep.solution.commits
144-
){
145-
theStep.solution.commits.push(commit.hash.substr(0,7));
146-
}
147-
}
148-
}
149-
}else{
150-
// If it's level: add the commit hash (if the level has the commit key) and the content to the tutorial
151-
consttheLevel:T.Level|null=
152-
tutorial.levels.find(
153-
(level:T.Level)=>level.id===matches.groups.levelId
154-
)||null;
63+
constcommits:CommitLogObject=awaitgetCommits(config.config.repo.branch);
15564

156-
if(!theLevel){
157-
console.log(`Level${matches.groups.levelId} not found`);
158-
}else{
159-
if(_.has(theLevel,"tutorial.commits")){
160-
if(theLevel.setup){
161-
theLevel.setup.commits.push(commit.hash.substr(0,7));
162-
}
163-
}
164-
}
165-
}
166-
}
167-
}
65+
// Otherwise, continue with the other options
66+
consttutorial:T.Tutorial=awaitparse({
67+
text:_markdown,
68+
config,
69+
commits,
70+
});
16871

169-
// cleanup the submodules
170-
if(!isLocal){
171-
letcleanupErr;
172-
173-
if(isSubModule){
174-
cleanupErr=awaitcleanupFiles(workingDir);
72+
if(tutorial){
73+
if(options.output){
74+
awaitwrite(options.output,JSON.stringify(tutorial),"utf8");
17575
}else{
176-
cleanupErr=rmDir(path.join(process.cwd(),workingDir));
177-
}
178-
179-
if(cleanupErr){
180-
console.log(
181-
`Error when deleting temporary files on${
182-
isSubModule ?"module" :"folder"
183-
}${workingDir}.`
184-
);
76+
console.log(JSON.stringify(tutorial,null,2));
18577
}
18678
}
187-
188-
// const isValid = validate(tutorial);
189-
190-
// if (!isValid) {
191-
// console.log(JSON.stringify(validate.errors, null, 2));
192-
// return;
193-
// }
194-
195-
returntutorial;
19679
}
19780

19881
exportdefaultbuild;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp