diff --git a/bin/index.js b/bin/index.js index 7400cf2..581b152 100755 --- a/bin/index.js +++ b/bin/index.js @@ -59,15 +59,15 @@ 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", @@ -75,5 +75,5 @@ yargs(hideBin(process.argv)) }); }, (argv) => { installFromPackageJson(argv.packageJson, argv.destination_folder) - }) + }) .parse(); \ No newline at end of file diff --git a/src/installFromPackagejson.js b/src/installFromPackagejson.js index d982328..4498024 100644 --- a/src/installFromPackagejson.js +++ b/src/installFromPackagejson.js @@ -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 + ); } } diff --git a/src/packagejsonmanip.js b/src/packagejsonmanip.js index a5356aa..738646b 100644 --- a/src/packagejsonmanip.js +++ b/src/packagejsonmanip.js @@ -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; } } diff --git a/tests/packagejsonmanip.test.js b/tests/packagejsonmanip.test.js new file mode 100644 index 0000000..cda1269 --- /dev/null +++ b/tests/packagejsonmanip.test.js @@ -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'); + }); + }); +});