Skip to content

Commit

Permalink
v1.1 command line tool (start)
Browse files Browse the repository at this point in the history
* load cmd
* commit cmd
* detect that git repo was already created
  • Loading branch information
Eccenux committed Oct 21, 2023
1 parent 60f75e6 commit b23d1fd
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wiki-to-git",
"version": "1.0.2",
"version": "1.1.0",
"description": "Node.js tool that helps to download Mediwiki page history and push it to a Git repository.",
"type": "module",
"main": "src/main.js",
Expand Down
34 changes: 28 additions & 6 deletions src/LoadData.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ export class LoadData {
* Init.
* @param {String} site Domain of the Wiki.
*/
constructor(site) {
constructor(site='') {
/** Base, output directory. */
this.baseDir = 'repo/';
/** History file. */
this.historyFile = 'history.json';
/** Initial URL. */
this.origin = `https://${site}`;
this.origin = !site.length ? '' : `https://${site}`;
/**
* Page history.
* @type {HistoryEntry[]}
Expand All @@ -35,6 +35,9 @@ export class LoadData {
* @param {Object} userOptions Limits.
*/
async load(page, userOptions) {
if (!this.history.length) {
throw 'You must set site (or origin) before trying to load.';
}
let options = {
pages: -1, // no. pages (default - no limit)
changes: -1, // no. changes (default - no limit)
Expand Down Expand Up @@ -172,6 +175,9 @@ export class LoadData {
if (!this.history.length) {
throw 'Load (or read) history first';
}
if (this.isGitDir()) {
console('Already exists.');
}
this.prepareDir();
const git = new GitOps(this.baseDir, repoName);
await git.create();
Expand Down Expand Up @@ -220,14 +226,27 @@ export class LoadData {
fs.mkdirSync(dir);
}
}
/**
* @private Is the base dir a git repo already?
*/
isGitDir() {
const dir = this.baseDir + '/.git';
if (fs.existsSync(dir)){
return true;
}
return false;
}

/**
* Save history as a file.
*/
async saveHistory() {
async saveHistory(historyFile='') {
if (!historyFile.length) {
historyFile = this.historyFile;
}
this.prepareDir();
const dir = this.baseDir;
const dstFile = dir + this.historyFile;
const dstFile = dir + historyFile;
const data = JSON.stringify(this.history, null, '\t');
await fsa.writeFile(dstFile, data);
return data;
Expand All @@ -236,9 +255,12 @@ export class LoadData {
/**
* Read history from a file.
*/
async readHistory() {
async readHistory(historyFile='') {
if (!historyFile.length) {
historyFile = this.historyFile;
}
const dir = this.baseDir;
const file = dir + this.historyFile;
const file = dir + historyFile;
const raw = await fsa.readFile(file, 'utf8');
this.history = JSON.parse(raw);
return this.history;
Expand Down
29 changes: 29 additions & 0 deletions src/cmd-commit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Commit history JSON to git.
*/
import { LoadData } from './LoadData.js';

//
// Config.
// change this values to your needs
const repoName = 'wiki-exampleGadgetScript';
const filename = 'exampleGadgetScript.js';
// optional JSON file name (default: 'history.json')
const historyFile = '';

const loader = new LoadData();

/**
* Read page history from JSON.
*/
loader.history = [];
await loader.readHistory(historyFile);
loader.info();

/**
* Create Git repo.
*/
console.log('\n\nCreating Git repo (%s).', repoName);
loader.repoCreate(repoName);
console.log('\nSave history as %s.', filename);
loader.repoCommit(repoName, filename);
25 changes: 25 additions & 0 deletions src/cmd-load.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Download page history from a MediaWiki site.
*/
import { LoadData } from './LoadData.js';

//
// Config.
// change this values to your needs
const site = 'en.wikipedia.org';
const page = 'MediaWiki:Gadget-exampleGadgetScript.js';
// optional JSON file name (default: 'history.json')
const historyFile = '';

const loader = new LoadData(site);

/**
* Download page history from a MediaWiki site.
*/
console.log('\n\nDownload history for %s.', page);
// this will load version history into internals
await loader.load(page);
// this will save history as JSON
await loader.saveHistory(historyFile);
// this just shows a quick info (you can skip this)
loader.info();

0 comments on commit b23d1fd

Please sign in to comment.