Skip to content

Commit

Permalink
Merge pull request #4 from ColinFay/install-from-packagejson-reborn
Browse files Browse the repository at this point in the history
feat: installFromPackage.json
  • Loading branch information
ColinFay authored Jun 10, 2024
2 parents 28fd78a + b174242 commit c22f8d9
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 13 deletions.
21 changes: 20 additions & 1 deletion bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { hideBin } = require('yargs/helpers')

const { install, installFromDesc } = require('../src/install')
const { init } = require('../src/init')
const { installFromPackageJson } = require('../src/installFromPackagejson')

yargs(hideBin(process.argv))
.command(
Expand Down Expand Up @@ -56,5 +57,23 @@ yargs(hideBin(process.argv))
(argv) => {
installFromDesc(argv.description_file, argv.destination_folder)
}
)
).
command(
'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.',
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();
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webrcli",
"version": "0.0.4",
"version": "0.0.5",
"description": "cli tools for building node project with webr inside",
"main": "index.js",
"bin": {
Expand Down
12 changes: 11 additions & 1 deletion src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@ const init = async (destination_folder) => {
process.chdir(destination_folder);

// installing npm dependencies
log("⏳ launching npm init, installing webr & spidyr----");
execSync(`npm init -y`);
execSync(`npm install webr spidyr`);

// append to package.json
let packageJson = JSON.parse(fs.readFileSync("package.json"));
packageJson.scripts["postinstall"] = "webrcli installFromPackageJson";
fs.writeFileSync("package.json", JSON.stringify(packageJson, null, 2));

process.chdir(previousDirectory);

// copying template
log("👉 Copying template ----");
log("💀 Copying project skeleton ----");
fs.cpSync(
path.join(__dirname, "..", "template"),
path.join(destination_folder),
Expand All @@ -44,9 +50,13 @@ const init = async (destination_folder) => {
webr_packages
)

process.chdir(destination_folder);

log("👉 Installing {pkgload} ----");
await installIt("pkgload", webr_packages)

process.chdir(previousDirectory);

return;

}
Expand Down
3 changes: 3 additions & 0 deletions src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const fs = require("fs");

const { installOnePackage } = require('./install-utils');
const { create_mount_folder_init_webr } = require('./config-utils.js');
const { append_package_json } = require('./packagejsonmanip');

const installIt = async function (
package_to_install,
Expand Down Expand Up @@ -44,6 +45,8 @@ const installIt = async function (
)
}

append_package_json(package_to_install)

await webR.close();

return true
Expand Down
28 changes: 28 additions & 0 deletions src/installFromPackagejson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { installIt } = require('./install');
const { parsePackageJson } = require('./packagejsonmanip');

function installFromPackageJson(
package_json = "package.json",
destination_folder = "webr_packages"
) {
// Read the package.json file
const package_json_read = parsePackageJson(package_json);
const dependencies = package_json_read.rdependencies;

if (dependencies) {
// Install the dependencies
for (let dependency of dependencies) {
installIt(
dependency,
destination_folder
);
}
} else {
console.log("❗️ No rdependencies found in the package.json file");
}

}

module.exports = {
installFromPackageJson: installFromPackageJson
}
58 changes: 58 additions & 0 deletions src/packagejsonmanip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const fs = require('fs');

function parsePackageJson(filePath) {
try {
const fileContent = fs.readFileSync(filePath, 'utf8');
const packageJson = JSON.parse(fileContent);
return packageJson;
} catch (error) {
console.error('Error parsing' + filePath, error);
return null;
}
}

function writePackageJson(filePath, packageJson) {
try {
const fileContent = JSON.stringify(packageJson, null, 2);
fs.writeFileSync(filePath, fileContent, 'utf8');
} catch (error) {
console.error('Error writing package.json:', error);
}
}

function addDependencies(
dependencies,
packageJson = "package.json"
) {
const pk = parsePackageJson(packageJson)
if (!pk.rdependencies) {
pk.rdependencies = []
}
pk.rdependencies.push(dependencies)

writePackageJson(packageJson, pk)
}

const append_package_json = function (package_to_add) {
const package_json_path = process.cwd() + "/package.json";
if (fs.existsSync(package_json_path)) {
const package_json = JSON.parse(fs.readFileSync(package_json_path));

if (!package_json.rdependencies) {
package_json.rdependencies = [];
}

package_json.rdependencies.push(package_to_add);
package_json.rdependencies = [...new Set(package_json.rdependencies)]
fs.writeFileSync(package_json_path, JSON.stringify(package_json, null, 2));
} else {
console.log("❗️ No package.json file found");
}
}

module.exports = {
parsePackageJson,
writePackageJson,
addDependencies,
append_package_json
};
1 change: 1 addition & 0 deletions tests/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const fs = require('fs');
test('init works', async () => {
// set a temp dir
let temp_dir = path.join(os.tmpdir(), 'coucou')
process.cwd = jest.fn().mockReturnValue(temp_dir);

if (
fs.existsSync(temp_dir)
Expand Down
2 changes: 1 addition & 1 deletion tests/install-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test('installOnePackage works', async () => {
let pkgname = 'sudoku';
let url = 'https://cran.r-project.org/src/contrib/sudoku_2.8.tar.gz';
let destination_folder = path.join(os.tmpdir(), 'sudoku');

process.cwd = jest.fn().mockReturnValue(destination_folder);
if (
fs.existsSync(destination_folder)
) {
Expand Down
2 changes: 1 addition & 1 deletion tests/install.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { timeLog } = require('console');

test('installIt works', async () => {
let temp_dir = path.join(os.tmpdir(), 'install');

process.cwd = jest.fn().mockReturnValue(temp_dir);
if (
fs.existsSync(temp_dir)
) {
Expand Down
75 changes: 75 additions & 0 deletions tests/packagejsonmanip.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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', () => {
let filePath = path.join(os.tmpdir(), 'parsePackageJson.json');
let packageJson = {
"name": 'my-package',
"version": '1.0.0',
"dependencies": {
"lodash": '^4.17.21',
"axios": '^0.21.1'
}
};
fs.writeFileSync(filePath, JSON.stringify(packageJson), 'utf8');

let result = packageJsonManip.parsePackageJson(filePath);

expect(result).toEqual(packageJson);

fs.unlinkSync(filePath);
});
});

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(), 'writePackageJson.json');
packageJsonManip.writePackageJson(temp_file, packageJson);

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

fs.unlinkSync(temp_file);
});
});

describe('addDependencies', () => {
it('should add the provided dependencies to the package.json file', () => {
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);

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

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

expect(result.rdependencies).toContain('golem');

fs.unlinkSync(temp_file);
});
});
});

0 comments on commit c22f8d9

Please sign in to comment.