Move private-cli commands to local-cli

Summary: public

We cannot remove `local-cli` because is referenced by the global cli explicitly. If we do so, people would have to upgrate this global thin cli which will cause some pain. So, lets move `private-cli` commands into `local-cli` instead.

Reviewed By: frantic

Differential Revision: D2571983

fb-gh-sync-id: 712c29430203660fb6f0d5f23813cb2a7156ee48
This commit is contained in:
Martín Bigio
2015-10-26 07:55:29 -07:00
committed by facebook-github-bot-3
parent 24537e3726
commit 849aa4dae6
33 changed files with 49 additions and 138 deletions

69
local-cli/util/Config.js Normal file
View File

@@ -0,0 +1,69 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
const fs = require('fs');
const path = require('path');
const RN_CLI_CONFIG = 'rn-cli.config.js';
let cachedConfig = null;
/**
* Module capable of getting the configuration that should be used for
* the `rn-cli`. The configuration file is a JS file named `rn-cli.config.js`.
* It has to be on any parent directory of the cli.
*
* The function will return all the default configuration functions overriden
* by those found on `rn-cli.config.js`, if any. If no default config is
* provided and no configuration can be found in the directory hierarchy an
* error will be thrown.
*/
const Config = {
get(cwd, defaultConfig) {
if (cachedConfig) {
return cachedConfig;
}
const parentDir = findParentDirectory(cwd, RN_CLI_CONFIG);
if (!parentDir && !defaultConfig) {
throw new Error(
'Can\'t find "rn-cli.config.js" file in any parent folder of "' +
__dirname + '"'
);
}
const config = parentDir
? require(path.join(parentDir, RN_CLI_CONFIG))
: {};
cachedConfig = Object.assign({}, defaultConfig, config);
cachedConfig.cwd = cwd;
return cachedConfig;
}
};
// Finds the most near ancestor starting at `currentFullPath` that has
// a file named `filename`
function findParentDirectory(currentFullPath, filename) {
const root = path.parse(currentFullPath).root;
const testDir = (parts) => {
if (parts.length === 0) {
return null;
}
const fullPath = path.join(root, parts.join(path.sep));
var exists = fs.existsSync(path.join(fullPath, filename));
return exists ? fullPath : testDir(parts.slice(0, -1));
};
return testDir(currentFullPath.substring(1).split(path.sep));
}
module.exports = Config;

View File

@@ -0,0 +1,12 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
module.exports.out = () => jest.genMockFn();
module.exports.err = () => jest.genMockFn();

View File

@@ -0,0 +1,30 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
const fs = require('fs');
function copyAndReplace(src, dest, replacements) {
console.log('src', src);
console.log('dest', dest);
if (fs.lstatSync(src).isDirectory()) {
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest);
}
} else {
let content = fs.readFileSync(src, 'utf8');
Object.keys(replacements).forEach(regex =>
content = content.replace(new RegExp(regex, 'g'), replacements[regex])
);
fs.writeFileSync(dest, content);
}
}
module.exports = copyAndReplace;

View File

@@ -0,0 +1,31 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
const fetch = require('node-fetch');
/**
* Indicates whether or not the packager is running. It ruturns a promise that
* when fulfilled can returns one out of these possible values:
* - `running`: the packager is running
* - `not_running`: the packager nor any process is running on the expected
* port.
* - `unrecognized`: one other process is running on the port ew expect the
* packager to be running.
*/
function isPackagerRunning() {
return fetch('http://localhost:8081/status').then(
res => res.text().then(body =>
body === 'packager-status:running' ? 'running' : 'unrecognized'
),
() => 'not_running'
);
}
module.exports = isPackagerRunning;

View File

@@ -0,0 +1,15 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
function isValidPackageName(name) {
return name.match(/^[$A-Z_][0-9A-Z_$]*$/i);
}
module.exports = isValidPackageName;

19
local-cli/util/log.js Normal file
View File

@@ -0,0 +1,19 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
function log(stream, module) {
return function() {
const message = Array.prototype.slice.call(arguments).join(' ');
stream.write(module + ': ' + message + '\n');
};
}
module.exports.out = log.bind(null, process.stdout);
module.exports.err = log.bind(null, process.stderr);

26
local-cli/util/walk.js Normal file
View File

@@ -0,0 +1,26 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
const fs = require('fs');
const path = require('path');
function walk(current) {
if (!fs.lstatSync(current).isDirectory()) {
return [current];
}
const files = fs.readdirSync(current).map(child => {
child = path.join(current, child);
return walk(child);
});
return [].concat.apply([current], files);
}
module.exports = walk;