Skip to content

Commit

Permalink
feat!: upgrade cjs to esm
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Werner committed Jan 2, 2024
1 parent 49dfb88 commit 8bf9b09
Show file tree
Hide file tree
Showing 33 changed files with 220 additions and 133 deletions.
11 changes: 5 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const FSLock = require('./src/FSLock/FSLock')
const Directory = require('./src/Directory/Directory')
const File = require('./src/File/File')
module.exports = {
FSLock, File, Directory
}
import FSLock from './src/FSLock/FSLock.js';
import Directory from './src/Directory/Directory.js';
import File from './src/File/File.js';

export {FSLock, File, Directory};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"bugs": {
"url": "https://github.com/Alex-Werner/FSlock/issues"
},
"type": "module",
"keywords": [
"queue",
"job",
Expand Down
19 changes: 13 additions & 6 deletions src/Directory/Directory.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import create from './methods/create.js';
import exists from './methods/exists.js';
import ensure from './methods/ensure.js';
import list from './methods/list.js';
import remove from './methods/remove.js';

const Directory = {};
Directory.create = require('./methods/create').bind(Directory);
Directory.exists = require('./methods/exists').bind(Directory);
Directory.ensure = require('./methods/ensure').bind(Directory);
Directory.list = require('./methods/list').bind(Directory);
Directory.remove = require('./methods/remove').bind(Directory);
module.exports = Directory;

Directory.create = create.bind(Directory);
Directory.exists = exists.bind(Directory);
Directory.ensure = ensure.bind(Directory);
Directory.list = list.bind(Directory);
Directory.remove = remove.bind(Directory);
export default Directory;
6 changes: 2 additions & 4 deletions src/Directory/Directory.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const { expect } = require('chai');
const path = require('path');
const File = require('../File/File');
const Directory = require('./Directory');
import { expect } from 'chai';
import Directory from './Directory.js';

describe('directory', () => {
it('should have a valid structure', () => {
Expand Down
9 changes: 6 additions & 3 deletions src/Directory/methods/create.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs');
const path = require('path');
module.exports = async function create(p) {
import fs from 'fs';
import path from 'path';

async function create(p) {
const self = this;
return new Promise((resolve, reject) => {
fs.mkdir(p, async (err) => {
Expand All @@ -16,3 +17,5 @@ module.exports = async function create(p) {
});
});
}

export default create;
4 changes: 3 additions & 1 deletion src/Directory/methods/ensure.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = async function ensure(p) {
async function ensure(p) {
const exist = await this.exists(p);

if(!exist){
Expand All @@ -9,3 +9,5 @@ module.exports = async function ensure(p) {
}
return exist;
}

export default ensure;
7 changes: 5 additions & 2 deletions src/Directory/methods/exists.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs');
module.exports = async function exists(p) {
import fs from 'fs';

async function exists(p) {
return new Promise((resolve, reject) => {
fs.stat(p, (err, stats) => {
if (err && err.code === 'ENOENT') {
Expand All @@ -14,3 +15,5 @@ module.exports = async function exists(p) {
});
});
}

export default exists;
7 changes: 5 additions & 2 deletions src/Directory/methods/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs');
module.exports = async function list(p = '') {
import fs from 'fs';

async function list(p = '') {
return new Promise((resolve, reject) => {
fs.readdir(p, (err, list) => {
if (err && err.code === 'ENOENT') {
Expand All @@ -11,3 +12,5 @@ module.exports = async function list(p = '') {
});
});
}

export default list;
10 changes: 7 additions & 3 deletions src/Directory/methods/remove.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs');
const path = require('path')
module.exports = async function remove(p) {
import fs from 'fs';
import path from 'path';

async function remove(p) {
const files = await this.list(p);
return new Promise((resolve, reject) => {
// If there is file, we remove them first
Expand Down Expand Up @@ -53,3 +54,6 @@ module.exports = async function remove(p) {
})
});
}


export default remove;
61 changes: 35 additions & 26 deletions src/FSLock/FSLock.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import add from './methods/add.js';
import get from './methods/get.js';
import processNext from './methods/processNext.js';
import processAll from './methods/processAll.js';
import start from './methods/start.js';
import stop from './methods/stop.js';

/**
* FSQueue
*
Expand All @@ -6,36 +13,37 @@
* Ideally, we should have way to add to the
*
*/
class FSLock{
constructor(props = {}){
const defaultProps = {
options:{
autoexec:true,
concurrency: null,//Infinite concurrent processes
timeout: 5000 //MS wait for execution
}
}
this.queue = [];
this.locks = {};
this.options = {
autoexec: (props.autoexec!==undefined) ? props.autoexec : defaultProps.options.autoexec
};
this.state = 'idle';
this.autoExecStarted = false;
class FSLock {
constructor(props = {}) {
const defaultProps = {
options: {
autoexec: true,
concurrency: null,//Infinite concurrent processes
timeout: 5000 //MS wait for execution
}
}
this.queue = [];
this.locks = {};
this.options = {
autoexec: (props.autoexec !== undefined) ? props.autoexec : defaultProps.options.autoexec
};
this.state = 'idle';
this.autoExecStarted = false;

if(this.options.autoexec){
this.start();
if (this.options.autoexec) {
this.start();
}
}
}
};

// Elements added to the queue will then need to be executed with manually except if autoexec
// Return a job
FSLock.prototype.add = require('./methods/add');
FSLock.prototype.get = require('./methods/get');
FSLock.prototype.processNext = require('./methods/processNext');
FSLock.prototype.processAll = require('./methods/processAll');
FSLock.prototype.start = require('./methods/start');
FSLock.prototype.stop = require('./methods/stop');
FSLock.prototype.add = add;
FSLock.prototype.get = get;
FSLock.prototype.processNext = processNext;
FSLock.prototype.processAll = processAll;
FSLock.prototype.start = start;
FSLock.prototype.stop = stop;

// Remove last job in queue
// FSQueue.prototype.pop = require('./methods/pop');
Expand All @@ -45,4 +53,5 @@ FSLock.prototype.stop = require('./methods/stop');

// Try to execute a passed job in first
// FSQueue.prototype.exec = require('./methods/exec');
module.exports = FSLock;

export default FSLock;
9 changes: 5 additions & 4 deletions src/FSLock/FSLock.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const {expect} = require('chai');
const FSLock = require('../../src/FSLock/FSLock');
const Job = require('../../src/Job/Job');
const Directory = require('../../src/Directory/Directory');
import {expect} from 'chai';

import FSLock from '../../src/FSLock/FSLock.js';
import Job from '../../src/Job/Job.js';
import Directory from '../../src/Directory/Directory.js';

describe('FSQueue', function suite() {
this.timeout(10000);
Expand Down
7 changes: 5 additions & 2 deletions src/FSLock/methods/add.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const Job = require('../../Job/Job');
module.exports = function add(command, path, params){
import Job from '../../Job/Job.js';

function add(command, path, params){
const job = new Job({command, path, params});
this.queue.push(job);
job.state = 'queued';
return job;
}

export default add;
4 changes: 3 additions & 1 deletion src/FSLock/methods/get.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module.exports = function get(index = 0) {
function get(index = 0) {
return this.queue[index];
}

export default get;
8 changes: 5 additions & 3 deletions src/FSLock/methods/ops/execCommand.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const File = require('../../../File/File');
const Directory = require('../../../Directory/Directory');
import File from '../../../File/File.js';
import Directory from '../../../Directory/Directory.js';

const utils = {File, Directory}
module.exports = async function execCommand(command, path, params){
async function execCommand(command, path, params){
let result, error;
try{
const [type,fn] = command.split('.');
Expand All @@ -18,3 +18,5 @@ module.exports = async function execCommand(command, path, params){
}
return {result, error};
}

export default execCommand;
4 changes: 3 additions & 1 deletion src/FSLock/methods/processAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const processQueue = async (self) => {
}
};

module.exports = async function processAll() {
async function processAll() {
this.state = 'processingAll';
const self = this;
if (this.queue.length === 0) {
Expand All @@ -19,3 +19,5 @@ module.exports = async function processAll() {
await processQueue(self);
this.state = 'idle';
};

export default processAll;
6 changes: 4 additions & 2 deletions src/FSLock/methods/processNext.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const execCommand = require('./ops/execCommand');
import execCommand from './ops/execCommand.js';
/**
*
* @param index {default:0} - Specify which index to process
* @returns {Promise<boolean>}
*/
module.exports = async function processNext(index=0, tries=0) {
async function processNext(index=0, tries=0) {
const self = this;
return new Promise(async (resolve, reject) => {
self.state = 'processing';
Expand Down Expand Up @@ -55,3 +55,5 @@ module.exports = async function processNext(index=0, tries=0) {
return resolve(true);
})
};

export default processNext;
4 changes: 3 additions & 1 deletion src/FSLock/methods/start.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = function start() {
function start() {
const self = this;
if (!this.autoExecStarted) this.autoExecStarted = true;

Expand All @@ -17,3 +17,5 @@ module.exports = function start() {
}
continuouslyExecute();
}

export default start;
4 changes: 3 additions & 1 deletion src/FSLock/methods/stop.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module.exports = function stop() {
function stop() {
if(!this.autoExecStarted) return false;
this.autoExecStarted = false;
}

export default stop;
30 changes: 21 additions & 9 deletions src/File/File.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
const File = {};
File.append = require('./methods/append').bind(File);
File.appendJSON = require('./methods/appendJSON').bind(File);
File.create = require('./methods/create').bind(File);
File.download = require('./methods/download').bind(File);
File.exists = require('./methods/exists').bind(File);
File.ensure = require('./methods/ensure').bind(File);
File.read = require('./methods/read').bind(File);
File.remove = require('./methods/remove').bind(File);
module.exports = File;

import append from "./methods/append.js";
import appendJSON from "./methods/appendJSON.js";
import create from "./methods/create.js";
import download from "./methods/download.js";
import exists from "./methods/exists.js";
import ensure from "./methods/ensure.js";
import read from "./methods/read.js";
import remove from "./methods/remove.js";

File.append = append.bind(File);
File.appendJSON = appendJSON.bind(File);
File.create = create.bind(File);
File.download = download.bind(File);
File.exists = exists.bind(File);
File.ensure = ensure.bind(File);
File.read = read.bind(File);
File.remove = remove.bind(File);


export default File;
7 changes: 3 additions & 4 deletions src/File/File.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const { expect } = require('chai');
const path = require('path');
const File = require('../../src/File/File');
const Directory = require('../../src/Directory/Directory');
import { expect } from 'chai';
import File from '../../src/File/File.js';
import Directory from '../../src/Directory/Directory.js';

describe('File', () => {
describe('file', function suite() {
Expand Down
5 changes: 3 additions & 2 deletions src/File/methods/append.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const fs = require('fs');
module.exports = async function append(p) {
import fs from 'fs';
async function append(p) {
return new Promise((res, rej) => {
fs.appendFile(p, data, (err) => {
if (err) rej(err);
res(true);
});
});
}
export default append;
10 changes: 6 additions & 4 deletions src/File/methods/appendJSON.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module.exports = async function append(p, data = {}) {
async function appendJSON(p, data = {}) {
const self = this;

return new Promise(async (resolve, reject) => {
let json = {};
if (await this.exists(p)) {
json = await this.read(p);
if (await self.exists(p)) {
json = await self.read(p);
}
const res = await this.create(p, Object.assign({}, json, data));
const res = await self.create(p, Object.assign({}, json, data));
resolve(res)
});
}

export default appendJSON;
Loading

0 comments on commit 8bf9b09

Please sign in to comment.