Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
Latest commit
executable file
·128 lines (117 loc) · 4.55 KB
/
Copy pathcli.js
File metadata and controls
executable file
·128 lines (117 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env node
varfs=require('fs');
varpath=require('path');
varos=require('os');
varfsExtra=require('fs-extra');
varyargs=require('yargs');
varchild_process=require('child_process');
varCONTEXT_HOME=path.join(os.homedir(),'.context','@plotdb','guides');
varcmds={};
cmds.init={
command: 'init',
desc: 'initialize context directory with shared symlink to @plotdb/guides/src',
builder: function(yargs){
returnyargs.option('global',{
alias: 'g',
type: 'boolean',
description: 'link context/shared to the globally installed @plotdb/guides package'
});
},
handler: function(argv){
varcwd=process.cwd();
varcontextDir=path.join(cwd,'context');
varsharedLink=path.join(contextDir,'shared');
varisGlobal=argv.global||false;
varsourceAbs,sourceDisplay;
if(isGlobal){
sourceAbs=path.join(CONTEXT_HOME,'src');
sourceDisplay=CONTEXT_HOME+'/src';
if(!fs.existsSync(sourceAbs)){
if(!fs.existsSync(CONTEXT_HOME)){
console.log('Cloning @plotdb/guides to '+CONTEXT_HOME+' ...');
fsExtra.ensureDirSync(path.dirname(CONTEXT_HOME));
try{
child_process.execSync('git clone https://github.com/plotdb/guides '+CONTEXT_HOME,{stdio: 'inherit'});
}catch(e){
console.error('Failed to clone @plotdb/guides: '+e.message);
process.exit(1);
}
}
if(!fs.existsSync(sourceAbs)){
console.error('Could not find src/ in '+CONTEXT_HOME);
process.exit(1);
}
}
}else{
sourceAbs=path.join(cwd,'node_modules','@plotdb','guides','src');
sourceDisplay=path.relative(cwd,sourceAbs);
if(!fs.existsSync(sourceAbs)){
console.error('Local @plotdb/guides was not found.\n\nInstall it locally:\n npm install -D github:plotdb/guides\n\nOr use the globally installed package:\n npx @plotdb/guides init -g');
process.exit(1);
}
}
fsExtra.ensureDirSync(contextDir);
// handle context/shared
varlinkStatus='created';
try{
varstat=fs.lstatSync(sharedLink);
if(stat.isSymbolicLink()){
varcurrent=fs.readlinkSync(sharedLink);
varcurrentAbs=path.resolve(contextDir,current);
if(currentAbs===sourceAbs){
linkStatus='already configured';
}else{
fs.unlinkSync(sharedLink);
linkStatus='updated';
}
}else{
console.error('context/shared already exists and is not a symbolic link.\n\nMove or remove it manually before running init again.');
process.exit(1);
}
}catch(e){
// lstatSync threw: path doesn't exist, proceed to create
}
if(linkStatus!=='already configured'){
varlinkTarget=isGlobal ? sourceAbs : path.relative(contextDir,sourceAbs);
fs.symlinkSync(linkTarget,sharedLink);
}
// update .gitignore
vargitignorePath=path.join(cwd,'.gitignore');
vargitignoreStatus='updated';
vargitignoreEntry='/context/shared';
varexisting='';
if(fs.existsSync(gitignorePath)){
existing=fs.readFileSync(gitignorePath,'utf8');
}
if(existing.split('\n').some(function(line){returnline.trim()===gitignoreEntry;})){
gitignoreStatus='already configured';
}else{
varappend=(existing.length&&!existing.endsWith('\n') ? '\n' : '')+gitignoreEntry+'\n';
fs.appendFileSync(gitignorePath,append,'utf8');
}
// create context/index.json
varindexPath=path.join(contextDir,'index.json');
varindexStatus;
if(fs.existsSync(indexPath)){
indexStatus='unchanged';
}else{
fs.writeFileSync(indexPath,JSON.stringify({
system: 'check path \'context/shared\' for context info (from github:plotdb/guides)'
},null,2)+'\n','utf8');
indexStatus='created';
}
console.log('@plotdb/guides initialized.\n');
console.log('Mode: '+(isGlobal ? 'global' : 'local'));
console.log('Source: '+sourceDisplay);
console.log('Link: context/shared ('+linkStatus+')');
console.log('Git ignore: '+gitignoreEntry+' ('+gitignoreStatus+')');
console.log('Context index: '+indexStatus);
}
};
vararg=yargs;
for(varkincmds){
arg=arg.command(cmds[k]);
}
arg.demandCommand(1,'Please specify a command.\n\nUsage:\n npx @plotdb/guides init [options]\n\nOptions:\n -g, --global\n Link context/shared to the globally installed @plotdb/guides package.')
.help()
.argv;