Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
Document code
Browse files Browse the repository at this point in the history
  • Loading branch information
jevakallio committed Nov 30, 2016
1 parent ddcd9bc commit 0ca1cfd
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 7 deletions.
23 changes: 22 additions & 1 deletion src/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,32 @@ const config = require('../util/config');
const log = require('../util/log');

const defaultConfig = {
// By default we only watch .js source files
include: '/**/*.js',
exclude: ['/node_modules/*', '.babelrc', '.git'],

exclude: [
// We do not want to sync node_modules. To sync changes to dependencies,
// use `whack install`
'/node_modules/*',

// Babel configuration should not be copied to avoid issues with
// locally installed not being present in the node_modules, which
// we do not syncronise. React Native packager does not support
// different presets for depdendencies anyway, do if the dependency
// does not correctly transpile with the parent's Babel config,
// the sources should be compiled and minified anyway
'.babelrc',

// Syncing git repos is unnecessary, and prevents npm from installing
// on top of the whacked directory
'.git'
],
dependencies: {}
};

/*
* Create a new whackage.json in current directory
*/
module.exports = function init({ force }) {
if (!force) {
assert.whackageJsonDoesntExist();
Expand Down
10 changes: 9 additions & 1 deletion src/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,22 @@ const spawn = require('../util/spawn');

const SUCCESS = 0;

module.exports = function link({ packageName = null }) {
/*
* Install the npm dependendencies of a named package, or all
* linked packages, into the current project
*/
module.exports = function install({ packageName = null }) {
assert.whackageJsonExists();

const whackage = config.read();

// we expect the package to be part of the project's whackage.json
if (packageName && !(packageName in whackage.dependencies)) {
assert.exitWith(`Package ${packageName} not linked in whackage.json`);
}

// if package name is provided, install dependencies of that package.
// otherwise install all linked packages
const paths = packageName
? [whackage.dependencies[packageName]]
: Object.keys(whackage.dependencies).map((key) => whackage.dependencies[key]);
Expand Down
8 changes: 8 additions & 0 deletions src/commands/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ const path = require('path');

const SUCCESS = 0;

/*
* Adds the package at given path to project's whackage.json.
* The package name is read from the linked package's package.json,
* but can be overidden with --name parameter if necessary.
*/
module.exports = function link({ relativePath, name }) {
assert.whackageJsonExists();
assert.isValidModule(relativePath);
Expand All @@ -15,8 +20,11 @@ module.exports = function link({ relativePath, name }) {

assert.isOwnDependency(packageName);

// install transitive dependencies
spawn('npm', ['install', relativePath], (code) => {
if (code === SUCCESS) {

// write entry in whackage.json
config.update((whackage) => {
whackage.dependencies = whackage.dependencies || {};
whackage.dependencies[packageName] = relativePath;
Expand Down
10 changes: 8 additions & 2 deletions src/commands/run.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
const path = require('path');
const commandExists = require('command-exists');

const whack = require('../util/whack');
const startServer = require('../util/start-server');
const spawn = require('../util/spawn');
const log = require('../util/log');

/*
* Starts the whackage file watcher service and the react native packager
*/
module.exports = function run({ npmScript }) {

// rn-cli.config.js is the react native packager configuration file used to
// blacklist dependencies' node modules to avoid @providesModule naming collisions
const cliConfigPath = path.resolve(__dirname, '../packager/rn-cli.config.js');

commandExists('rsync', (error, rsyncExists) => {
if (!error && rsyncExists) {
whack();
startServer();
spawn('npm', [npmScript, '--', '--config', cliConfigPath], (code) => {
process.exit(code);
});

} else {
log.error(
'Could\'t find `rsync`. Install it using your favorite package manager and try again'
Expand Down
6 changes: 5 additions & 1 deletion src/commands/unlink.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const assert = require('../util/assert');
const config = require('../util/config');
const log = require('../util/log');
module.exports = function link({ packageName }) {

/*
* Remove a package from whackage.json by name
*/
module.exports = function unlink({ packageName }) {
assert.whackageJsonExists();
config.update((whackage) => {
whackage.dependencies = whackage.dependencies || {};
Expand Down
3 changes: 3 additions & 0 deletions src/util/spawn.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const childProcess = require('child_process');

/*
* Spawn a child process with a callback when the process exits
*/
module.exports = function spawn(task, args, onClose) {
const child = childProcess.spawn('npm', args, {
stdio: 'inherit'
Expand Down
8 changes: 6 additions & 2 deletions src/util/whack.js → src/util/start-server.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@

const path = require('path');
const chokidar = require('chokidar');
const assert = require('./assert');
const syncAll = require('./sync-all');
const syncFile = require('./sync-file');
const config = require('./config');

module.exports = function start() {
/*
* Starts the whackage file watching service. Syncs the whole directories
* when the service is started, and as files are changed, copies individual
* file modifications one by one
*/
module.exports = function startServer() {

const ROOT_PATH = process.cwd();

Expand Down
1 change: 1 addition & 0 deletions src/util/sync-all.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const path = require('path');
const rsync = require('rsyncwrapper');
const log = require('./log');

// eslint-disable-next-line max-params
module.exports = function syncAll(rootPath, source, name, exclude) {
const options = {
Expand Down

0 comments on commit 0ca1cfd

Please sign in to comment.