| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #ifndef NODEJS_H |
| 4 | #define NODEJS_H |
| 5 | |
| 6 | #include <QObject> |
| 7 | |
| 8 | class Settings; |
| 9 | class QProcess; |
| 10 | |
| 11 | class NodeJs : public QObject { |
| 12 | Q_OBJECT |
| 13 | |
| 14 | public: |
| 15 | struct PackageMetadata { |
| 16 | public: |
| 17 | // Name of package. |
| 18 | QString m_name; |
| 19 | |
| 20 | // Version description. This could be fixed version or empty |
| 21 | // string (latest version) or perhaps version range. |
| 22 | QString m_version; |
| 23 | }; |
| 24 | |
| 25 | enum class PackageStatus { |
| 26 | // Package not installed. |
| 27 | NotInstalled, |
| 28 | |
| 29 | // Package installed but out-of-date. |
| 30 | OutOfDate, |
| 31 | |
| 32 | // Package installed and up-to-date. |
| 33 | UpToDate |
| 34 | }; |
| 35 | |
| 36 | explicit NodeJs(Settings* settings, QObject* parent = nullptr); |
| 37 | |
| 38 | void runScript(QProcess* proc, const QString& script, const QStringList& arguments) const; |
| 39 | |
| 40 | QString nodeJsExecutable() const; |
| 41 | void setNodeJsExecutable(const QString& exe) const; |
| 42 | |
| 43 | QString npmExecutable() const; |
| 44 | void setNpmExecutable(const QString& exe) const; |
| 45 | |
| 46 | QString packageFolder() const; |
| 47 | QString processedPackageFolder() const; |
| 48 | void setPackageFolder(const QString& path); |
| 49 | |
| 50 | QString nodeJsVersion(const QString& nodejs_exe) const; |
| 51 | QString npmVersion(const QString& npm_exe) const; |
| 52 | |
| 53 | // Checks status of package. |
| 54 | // |
| 55 | // NOTE: https://docs.npmjs.com/cli/v8/commands/npm-ls |
| 56 | PackageStatus packageStatus(const PackageMetadata& pkg) const; |
| 57 | |
| 58 | // Installs package. |
| 59 | // |
| 60 | // If package is NOT installed, then it will be installed. |
| 61 | // If package IS installed but out-of-date, it is updated to desired versions. |
| 62 | // |
| 63 | // NOTE: https://docs.npmjs.com/cli/v8/commands/npm-install |
| 64 | void installUpdatePackages(const QObject* sndr, const QList<PackageMetadata>& pkgs); |
| 65 | |
| 66 | void installPackages(const QObject* sndr, const QList<PackageMetadata>& pkgs); |
| 67 | |
| 68 | static QString packagesToString(const QList<PackageMetadata>& pkgs); |
| 69 | |
| 70 | signals: |
| 71 | void packageError(const QObject* sndr, const QList<PackageMetadata>& pkgs, const QString& error); |
| 72 | void packageInstalledUpdated(const QObject* sndr, const QList<PackageMetadata>& pkgs, bool already_up_to_date); |
| 73 | |
| 74 | private: |
| 75 | Settings* m_settings; |
| 76 | }; |
| 77 | |
| 78 | #endif // NODEJS_H |