Spaces:
Sleeping
Sleeping
File size: 6,657 Bytes
7f21468 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
const path = require('path');
const util = require('util');
const filenamify = require('filenamify');
const findCacheDir = require('find-cache-dir');
const fs = require('fs-extra');
const globby = require('globby');
const Git = require('./git.js');
const copy = require('./util.js').copy;
const getUser = require('./util.js').getUser;
const log = util.debuglog('gh-pages');
/**
* Get the cache directory.
* @param {string} [optPath] Optional path.
* @return {string} The full path to the cache directory.
*/
function getCacheDir(optPath) {
const dir = findCacheDir({name: 'gh-pages'});
if (!optPath) {
return dir;
}
return path.join(dir, filenamify(optPath));
}
/**
* Clean the cache directory.
*/
exports.clean = function clean() {
fs.removeSync(getCacheDir());
};
exports.defaults = {
dest: '.',
add: false,
git: 'git',
depth: 1,
dotfiles: false,
branch: 'gh-pages',
remote: 'origin',
src: '**/*',
remove: '.',
push: true,
history: true,
message: 'Updates',
silent: false,
};
exports.getCacheDir = getCacheDir;
function getRepo(options) {
if (options.repo) {
return Promise.resolve(options.repo);
} else {
const git = new Git(process.cwd(), options.git);
return git.getRemoteUrl(options.remote);
}
}
/**
* Push a git branch to a remote (pushes gh-pages by default).
* @param {string} basePath The base path.
* @param {Object} config Publish options.
* @param {Function} callback Callback.
* @return {Promise} A promise.
*/
exports.publish = function publish(basePath, config, callback) {
if (typeof config === 'function') {
callback = config;
config = {};
}
const options = Object.assign({}, exports.defaults, config);
// For backward compatibility before fixing #334
if (options.only) {
options.remove = options.only;
}
if (!callback) {
callback = function (err) {
if (err) {
log(err.message);
}
};
}
function done(err) {
try {
callback(err);
} catch (err2) {
log('Publish callback threw: %s', err2.message);
}
}
try {
if (!fs.statSync(basePath).isDirectory()) {
const err = new Error('The "base" option must be an existing directory');
done(err);
return Promise.reject(err);
}
} catch (err) {
done(err);
return Promise.reject(err);
}
const files = globby
.sync(options.src, {
cwd: basePath,
dot: options.dotfiles,
})
.filter((file) => {
return !fs.statSync(path.join(basePath, file)).isDirectory();
});
if (!Array.isArray(files) || files.length === 0) {
done(
new Error('The pattern in the "src" property didn\'t match any files.'),
);
return;
}
let repoUrl;
let userPromise;
if (options.user) {
userPromise = Promise.resolve(options.user);
} else {
userPromise = getUser();
}
return userPromise.then((user) =>
getRepo(options)
.then((repo) => {
repoUrl = repo;
const clone = getCacheDir(repo);
log('Cloning %s into %s', repo, clone);
return Git.clone(repo, clone, options.branch, options);
})
.then((git) => {
return git.getRemoteUrl(options.remote).then((url) => {
if (url !== repoUrl) {
const message =
'Remote url mismatch. Got "' +
url +
'" ' +
'but expected "' +
repoUrl +
'" in ' +
git.cwd +
'. Try running the `gh-pages-clean` script first.';
throw new Error(message);
}
return git;
});
})
.then((git) => {
// only required if someone mucks with the checkout between builds
log('Cleaning');
return git.clean();
})
.then((git) => {
log('Fetching %s', options.remote);
return git.fetch(options.remote);
})
.then((git) => {
log('Checking out %s/%s ', options.remote, options.branch);
return git.checkout(options.remote, options.branch);
})
.then((git) => {
if (!options.history) {
return git.deleteRef(options.branch);
} else {
return git;
}
})
.then((git) => {
if (options.add) {
return git;
}
log('Removing files');
const files = globby
.sync(options.remove, {
cwd: path.join(git.cwd, options.dest),
})
.map((file) => path.join(options.dest, file));
if (files.length > 0) {
return git.rm(files);
} else {
return git;
}
})
.then((git) => {
if (options.nojekyll) {
log('Creating .nojekyll');
fs.createFileSync(path.join(git.cwd, '.nojekyll'));
}
if (options.cname) {
log('Creating CNAME for %s', options.cname);
fs.writeFileSync(path.join(git.cwd, 'CNAME'), options.cname);
}
log('Copying files');
return copy(files, basePath, path.join(git.cwd, options.dest)).then(
function () {
return git;
},
);
})
.then((git) => {
return Promise.resolve(
options.beforeAdd && options.beforeAdd(git),
).then(() => git);
})
.then((git) => {
log('Adding all');
return git.add('.');
})
.then((git) => {
if (!user) {
return git;
}
return git.exec('config', 'user.email', user.email).then(() => {
if (!user.name) {
return git;
}
return git.exec('config', 'user.name', user.name);
});
})
.then((git) => {
log('Committing');
return git.commit(options.message);
})
.then((git) => {
if (options.tag) {
log('Tagging');
return git.tag(options.tag).catch((error) => {
// tagging failed probably because this tag alredy exists
log(error);
log('Tagging failed, continuing');
return git;
});
} else {
return git;
}
})
.then((git) => {
if (options.push) {
log('Pushing');
return git.push(options.remote, options.branch, !options.history);
} else {
return git;
}
})
.then(
() => done(),
(error) => {
if (options.silent) {
error = new Error(
'Unspecified error (run without silent option for detail)',
);
}
done(error);
},
),
);
};
|