-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from ColinFay/install-from-packagejson-reborn
feat: installFromPackage.json
- Loading branch information
Showing
11 changed files
with
207 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |