Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinFay committed Mar 27, 2024
1 parent 1b0ef24 commit 1e08398
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 13 deletions.
12 changes: 6 additions & 6 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,21 @@ yargs(hideBin(process.argv))
}
).
command(
'installFromPackageJson [packageJson] [destination_folder]',
'Install dependencies from package.json file.',
'installFromPackageJson [packageJson] [destination_folder]',
'Install dependencies from package.json file containing an "rdependencies" entry.',
(yargs) => {
yargs.
positional('packageJson', {
type: 'string',
describe: 'Path to the package.json file.',
type: 'string',
describe: 'Path to the package.json file.',
default: "package.json",
})
})
.positional('destination_folder', {
type: 'string',
default: "./webr_packages",
describe: 'Where to install the packages',
});
}, (argv) => {
installFromPackageJson(argv.packageJson, argv.destination_folder)
})
})
.parse();
18 changes: 12 additions & 6 deletions src/installFromPackagejson.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
const fs = require('fs');
const installIt = require('./install');
const {parsePackageJson} = require('./packagejsonmanip');

function installFromPackageJson(destination_folder = "webr_packages") {
function installFromPackageJson(
package_json = "package.json",
destination_folder = "webr_packages"
) {
// Read the package.json file
const packageJson = fs.readFileSync('package.json', 'utf8');
const dependencies = JSON.parse(packageJson).rdependencies;

// Loop over the dependencies and run installIt for each entry
const package_json_read = parsePackageJson(package_json);
const dependencies = package_json_read.rdependencies;
// Install the dependencies
for (const dependency in dependencies) {
installIt(dependency);
installIt(
dependency,
destination_folder
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/packagejsonmanip.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function parsePackageJson(filePath) {
const packageJson = JSON.parse(fileContent);
return packageJson;
} catch (error) {
console.error('Error parsing package.json:', error);
console.error('Error parsing pouet.json:', error);
return null;
}
}
Expand Down
71 changes: 71 additions & 0 deletions tests/packagejsonmanip.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const fs = require('fs');
const packageJsonManip = require('../src/packagejsonmanip');
const path = require('path');
const os = require('os');

describe('packagejsonmanip', () => {
describe('parsePackageJson', () => {
it('should parse the package.json file and return an object', () => {
const filePath = 'path/to/package.json';
const packageJson = {
name: 'my-package',
version: '1.0.0',
dependencies: {
lodash: '^4.17.21',
axios: '^0.21.1'
}
};
fs.readFileSync = jest.fn().mockReturnValue(JSON.stringify(packageJson));

const result = packageJsonManip.parsePackageJson(filePath);

expect(result).toEqual(packageJson);
});
});

describe('writePackageJson', () => {
it('should write the package.json file with the provided content', () => {
const packageJson = {
name: 'my-package',
version: '1.0.0',
dependencies: {
lodash: '^4.17.21',
axios: '^0.21.1'
}
};
// create a temp json file
let temp_file = path.join(os.tmpdir(), 'package.json');
packageJsonManip.writePackageJson(temp_file, packageJson);

// expect the file to exist
const result = packageJsonManip.parsePackageJson(temp_file);
expect(result).toEqual(packageJson);
});
});

describe('addDependencies', () => {
it('should add the provided dependencies to the package.json file', () => {
expect(true).toEqual(true);
const packageJson = {
name: 'my-package',
version: '1.0.0',
dependencies: {
lodash: '^4.17.21',
axios: '^0.21.1'
}
};
// create a temp json file
let temp_file = path.join(os.tmpdir(), 'package.json');
console.log('ouetpu', temp_file);
packageJsonManip.writePackageJson(temp_file, packageJson);

// add a new dependency
packageJsonManip.addDependencies('golem', temp_file);

// expect the new dependency to be added
const result = packageJsonManip.parsePackageJson(temp_file);

expect(JSON.parse(result).rdependencies).toContain('golem');
});
});
});

0 comments on commit 1e08398

Please sign in to comment.