Files
react-native-devtools-frontend/scripts/test/run_lint_check_css.js
T
Jack Franklin bc30234f61 Migrate run_lint_check_css to JS script
As part of the work to move more scripts to Node, not Python, over time,
picked this one as the starting point. I changed its API slightly to
allow more flags to be taken in, as we'll need that to do a stylelint
pass against TypeScript files, but I will do that in a subsequent CL.

I had to make quite a few changes to devtools_paths.js, but I think it's
now calculating paths correctly. It took a bit of messing to get the
equivalent of Python's path.abspath(__file__), as you'll see from the
large comment that tries to explain what's going on!

Bug: chromium:1166108, chromium:1166572
Change-Id: Ia0b19ff8956b2ede2447530be57876a88046887e
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2631113
Commit-Queue: Jack Franklin <jacktfranklin@chromium.org>
Reviewed-by: Tim van der Lippe <tvanderlippe@chromium.org>
2021-01-18 10:30:12 +00:00

55 lines
1.7 KiB
JavaScript

// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
const path = require('path');
const childProcess = require('child_process');
const {
devtoolsRootPath,
stylelintExecutablePath,
nodePath,
} = require('../devtools_paths.js');
const yargsObject = require('yargs')
.option('files', {type: 'array', desc: 'One or more files to lint.'})
.option('glob', {default: '**/*.css', desc: 'A glob to choose which files to lint.'})
.argv;
// Note: stylelint requires POSIX-formatted paths/globs, even on Windows.
// The forward slash is not a bug.
const DEFAULT_GLOB = '**/*.css';
function getCSSFilesOrGlobList() {
const files = yargsObject.files || [];
if (files.length > 0) {
return files.map(file => {
// Enforce posix file paths even on Windows
return file.split(path.sep).join(path.posix.sep);
});
}
return [yargsObject.glob || DEFAULT_GLOB];
}
function run() {
// eslint-disable-next-line no-unused-vars
const {glob, files, _, $0, ...flagsForStylelint} = yargsObject;
/*
* This reduce is a flatten, because our Node version
* doesn't have flatMap :(
*/
const extraFlagsForStylelint =
Object.keys(flagsForStylelint).map(key => [`--${key}`, flagsForStylelint[key]]).reduce((a, b) => a.concat(b), []);
const args = [
stylelintExecutablePath(), ...getCSSFilesOrGlobList(), '--fix', '--allow-empty-input', ...extraFlagsForStylelint
];
const result =
childProcess.spawnSync(nodePath(), args, {encoding: 'utf-8', cwd: devtoolsRootPath(), stdio: 'inherit'});
process.exit(result.status);
}
run();